Leetcode 333: Largest BST Subtree

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 333: Largest BST Subtree相关的知识,希望对你有一定的参考价值。

 

Note:
A subtree must include all of its descendants.
Here‘s an example:

    10
    /    5  15
  / \   \ 
 1   8   7

The Largest BST Subtree in this case is the highlighted one. 
The return value is the subtree‘s size, which is 3.

 

Follow up:
Can you figure out ways to solve it with O(n) time complexity?

 

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left;
 6  *     public TreeNode right;
 7  *     public TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Result
11 {
12     public int lower;
13     public int upper;
14     public int count;
15     
16     public Result(int l, int u, int c)
17     {
18         lower = l;
19         upper = u;
20         count = c;
21     }
22 }
23 
24 // bottom up solution, the range is calculated from bottom, we need to use a special datastructure Result to implement it
25 public class Solution {
26     public int LargestBSTSubtree(TreeNode root) {
27         if (root == null) return 0;
28         var count = new int[1];
29         
30         CountBSTNodes(root, count); 
31         return count[0];           
32     }
33     
34     private Result CountBSTNodes(TreeNode node, int[] count)
35     {
36         if (node == null) return new Result(Int32.MaxValue, Int32.MinValue, 0);
37         
38         var left = CountBSTNodes(node.left, count);   
39         var right = CountBSTNodes(node.right, count);   
40         
41         if (left.count < 0 || right.count < 0 || node.val <= left.upper || node.val >= right.lower)
42         {
43             return new Result(0, 0, -1);
44         }
45         
46         count[0] = Math.Max(count[0], left.count + right.count + 1);
47         return new Result(Math.Min(left.lower, node.val), Math.Max(right.upper, node.val), left.count + right.count + 1);
48     }
49 }
50 
51 // top down solution
52 public class Solution1 {
53     public int LargestBSTSubtree(TreeNode root) {
54         if (root == null) return 0;
55         
56         int total = CountBSTNodes(root, Int32.MinValue, Int32.MaxValue); 
57         if (total >= 0) return total;
58         
59         return Math.Max(LargestBSTSubtree(root.left), LargestBSTSubtree(root.right));             
60     }
61     
62     // returns -1 if it‘s not a BST
63     private int CountBSTNodes(TreeNode node, int min, int max)
64     {
65         if (node == null) return 0;
66         
67         // quick cut
68         if (node.val <= min || node.val >= max) return -1;
69         
70         int left = CountBSTNodes(node.left, min, node.val);    
71         if (left < 0)
72         {
73             return -1;
74         }
75         
76         int right = CountBSTNodes(node.right, node.val, max);
77         if (right < 0)
78         {
79             return -1;
80         }
81         
82         return left + right + 1;
83     }
84 }

 





以上是关于Leetcode 333: Largest BST Subtree的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Largest BST Subtree

Leetcode: Largest BST Subtree

[LeetCode] Largest BST Subtree 最大的二分搜索子树

LeetCode "Largest BST Subtree"

java 333.最大的BST子树(#1).java

java 333.最大的BST子树(#1).java