1/**2* Definition for binary tree3* struct TreeNode {4* int val;5* TreeNode *left;6* TreeNode *right;7* TreeNode(int x) : val(x), left(NULL), right(NULL) {}8* };9*/10classSolution {11public:12TreeNode *sortedAr
LintCode "Remove Node in Binary Search Tree" > 日一二三四五六 1234567 891011121314 15161718192021 22232425262728 293012345 6789101112 Not hard to find a solution, but there are several corner cases. View Code
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...
So we can start b-search on string length of s1, for s2, since current substr len is fixed, we use rolling-hash to check match. Also, another interesting point to handle collision in a typical BK algorithm: we use double hashing to avoid collision, instead of a O(n) brutal-force str...
}public: vector<int> findMode(TreeNode*root) { auto h=go(root);intmcnt =0; vector<int>ret;for(auto &kv : h) {if(kv.second >mcnt) { mcnt=kv.second; ret={kv.first}; }elseif(kv.second ==mcnt) { ret.push_back(kv.first); ...
12 This is to test your knowledge on BST and its traversal. 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); ...
* TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };*/classSolution { vector<vector<int>>ret;intgo(TreeNode *p)//return max height{if(!p)return0;inthl = go(p->left);inthr = go(p->right);inth = max(hl, hr) +1;if(h >ret.size()) ret...
Recursion, a natural thought:class Solution { // Top : BottomRight std::pair rotate(TreeNode *p) { if (!p->left && !p->right) ...
Really interesting BST manipulation problem!class Solution {public: TreeNode *findConn(TreeNode *p) { TreeNode *pTmp = p; while (p...
LintCode "Binary Tree Serialization" 2025年5月> 日一二三四五六 27282930123 45678910 11121314151617 18192021222324 25262728293031 1234567 Here is a BFS solution which is compatible with LeetCode binary tree format. View Code