Not hard to find a solution, but there are several corner cases. View Code
One TopCoder article introduces a very interesting alternative solution to Longest Common Sequences problem. It is based on this statement (http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=stringSearching): The important point that allows us to use BS is the fact that if the gi...
Is it a binary tree Questions please seeHackerRank. It was too long. In simple, how can you check if a tree is a binary search tree? Idea Read the binary search tree from leftmost to rightmost, and make sure the order is always increasing. If you still remember how to do an in-orde...
Flatting BST into an array using in-order, and check that array. It is that simple: classSolution {public:voidserial(TreeNode *root, vector<int> &vec) {if(!root)return;if(root->left) serial(root->left, vec); vec.push_back(root->val);if(root->right) serial(root->right, vec); ...
typedef unordered_map<int, unsigned>HM;classSolution { HM go(TreeNode*p) {if(!p)returnHM(); auto rl= go(p->left); auto rr= go(p->right); HM r=rl;for(auto &kv : rr) r[kv.first] +=kv.second; r[p->val] ++;returnr; ...