Skip to content

Instantly share code, notes, and snippets.

@siddhantkushwaha
Last active October 17, 2019 19:11
Show Gist options
  • Save siddhantkushwaha/353d5be2461dea6d3ea5052c2b88c50e to your computer and use it in GitHub Desktop.
Save siddhantkushwaha/353d5be2461dea6d3ea5052c2b88c50e to your computer and use it in GitHub Desktop.
Some tricky tree implementations.
/*
Hash each subtree recursively and check if that hash already exists.
*/
string check_dup(node *root, set<string> &subtrees, int &duplicate_exists) {
if (root == NULL)
return "#";
string hash_left = check_dup(root->left, subtrees, duplicate_exists);
string hash_right = check_dup(root->right, subtrees, duplicate_exists);
string merged = to_string(root->val) + "_" + hash_left + "_" + hash_right;
if (root->left || root->right)
if (subtrees.count(merged) > 0)
duplicate_exists = true;
else
subtrees.insert(merged);
return merged;
}
int main() {
vector<int> arr = {20, 2, -1, -1, 2, -1, -1};
auto root = deserialize(arr);
int duplicate_exists = 0;
set<string> subtrees;
check_dup(root, subtrees, duplicate_exists);
cout << duplicate_exists << '\n';
}
int find_sum(node *root) {
if(root==NULL)
return 0;
return root->val + find_sum(root->left) + find_sum(root->right);
}
int find_subtree(node *p, node *root, int val, int &flag) {
if(root == NULL)
return 0;
int l = find_subtree(p, root->left, val, flag);
int r = find_subtree(p, root->right, val, flag);
int sum = root->val + l + r;
flag |= (sum == val) && (root != p);
return sum;
}
int Solution::solve(TreeNode* root) {
int sum = find_sum(root);
if(sum&1)
return 0;
else {
// a subtree should exist with half sum
int flag = 0;
find_subtree(root, root, sum/2, flag);
return flag;
}
}
void longest_consecutive_path(node *root, int parent_val, int current_path, int &ans) {
if (root == NULL)
return;
if(root->val - parent_val == 1)
current_path += 1;
else
current_path = 1;
ans = max(ans, current_path);
longest_consecutive_path(root->left, root->val, current_path, ans);
longest_consecutive_path(root->right, root->val, current_path, ans);
}
int main() {
vector<int> arr = {8, 9, 2, 3, 1, 5, 7, 4, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
auto root = deserialize(arr);
dfs(root);
int ans = 0;
longest_consecutive_path(root, -1, 1, ans);
cout << ans << '\n';
}
#include "bits/stdc++.h"
using namespace std;
struct node {
int val;
node *left;
node *right;
node(int val) {
this->val = val;
left = NULL;
right = NULL;
}
};
node *deserialize_util(vector<int> &arr, int &i) {
if (arr[i] == -1)
return NULL;
node *new_node = new node(arr[i]);
new_node->left = deserialize_util(arr, ++i);
new_node->right = deserialize_util(arr, ++i);
return new_node;
}
node *deserialize(vector<int> &arr) {
int i = 0;
return deserialize_util(arr, i);
}
void dfs_util(node *r) {
if (r == NULL)
return;
cout << r->val << ' ';
dfs_util(r->left);
dfs_util(r->right);
}
void dfs(node *r) {
dfs_util(r);
cout << '\n';
}
int main() {
vector<int> arr = {20, 2, -1, -1, 2, -1, -1};
auto root = deserialize(arr);
dfs(root);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment