Given a binary tree, find the largest subtree which is a binary Search tree (BST), where largest means subtree with larges T number of nodes in it.
Note:
A subtree must include all of its descendants.
Here's an example:
5/ \ \ 1 8 7
The largest BST subtree in the the highlighted one.
The return value is the subtree's size, which is 3.
Hint:
- You can recursively with algorithm similar to 98. Validate Binary Search tree at each node of the tree, which would result in O (NLOGN) time complexity.
Follow up:
Can you figure out the ways to solve it with O (n) time complexity?
This problem let us ask for a binary tree of the largest binary search sub-tree, the so-called binary search tree is to meet the left < Root < Right of the binary tree, we need to return the binary search subtree of the number of nodes. The tip of the topic said we can use the previous way validate Binary search tree method to do, time complexity of O (Nlogn), this method is to each node as the root node, to verify whether it is a two-fork search number, and record the number of nodes, if the two fork search tree, To update the final result, see the code below:
Solution One:
classSolution { Public: intLargestbstsubtree (treenode*root) { intres =0; DFS (root, res); returnRes; } voidDFS (TreeNode *root,int&Res) { if(!root)return; intD =Countbfs (Root, Int_min, Int_max); if(d! =-1) {res=Max (res, d); return; } dfs (Root-Left , RES); DFS (Root-Right , res); } intCOUNTBFS (TreeNode *root,intMnintmx) {if(!root)return 0; if(Root->val < MN | | root->val > MX)return-1; intleft = Countbfs (Root->left, MN, root->val); if(left = =-1)return-1; intright = Countbfs (Root->right, root->val, MX); if(Right = =-1)return-1; returnLeft + right +1; }};
The topic of follow up let us use O (n) time complexity to solve the problem, we still use the idea of Dfs, because of the limitations of time complexity, only allow us to traverse the entire binary tree, due to meet the requirements of the two-fork search subtree must be a leaf node, So our idea is to first recursive to the leftmost child node, and then to the top of the recursive, for each node, we record the current maximum number of BST nodes, as the maximum value of Zuozi, and as the minimum value of the right subtree, when each encounter left child node does not exist or the current node value is greater than the maximum left subtree, And if the right subtree does not exist or the current node value is less than the minimum number of right subtree, the number of nodes in BST is increased, we update the result and its parameters, if the current node is not a BST node, then we update the number of nodes of BST Res is a larger value of the number of nodes of the respective BST node of the left and See the code below:
Solution Two:
classSolution { Public: intLargestbstsubtree (treenode*root) { intres =0, MN = int_min, mx =Int_max; BOOLD =Isvalidbst (Root, MN, MX, res); returnRes; } BOOLIsvalidbst (TreeNode *root,int&MN,int&MX,int&Res) { if(!root)return true; intLeft_n =0, Right_n =0, left_mn =int_min; intRight_mn = int_min, left_mx = Int_max, right_mx =Int_max; BOOLleft = Isvalidbst (root->Left , Left_mn, Left_mx, left_n); BOOLright = Isvalidbst (root->Right , right_mn, Right_mx, right_n); if(Left &&Right ) { if((!root->left | | root->val >= left_mx) && (!root->right | | root->val <=right_mn)) {Res= Left_n + Right_n +1; MN= Root->left? Left_mn:root->Val; MX= Root->right? Right_mx:root->Val; return true; }} Res=Max (Left_n, right_n); return false; }};
Similar topics:
Validate Binary Search Tree
Resources:
Https://leetcode.com/discuss/85959/12ms-c-solution
Https://leetcode.com/discuss/86015/two-dfs-solution
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Largest BST subtree largest binary search subtree