333. Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note: A subtree must include all of its descendants. Example: Input: [10,5,15,1,8,null,7] 10 / \ 5 15 / \ \ 1 8 7 Output: 3 Explanation: 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? I feel this is post order traversal https://www.youtube.com/watch?v=4fiDs7CCxkc https://leetcode.com/problems/largest-bst-subtree/discuss/78891/Share-my-O(n)-Java-code-with-brief-explanation-and-comments
public class Solution { // return array for each node: // [0] --> min // [1] --> max // [2] --> largest BST in its subtree(inclusive) public int largestBSTSubtree(TreeNode root) { int[] ret = largestBST(root); return ret[2]; } private int[] largestBST(TreeNode node){ if(node == null){ return new int[]{Integer.MAX_VALUE, Integer.MIN_VALUE, 0}; } int[] left = largestBST(node.left); int[] right = largestBST(node.right); if(node.val > left[1] && node.val < right[0]){ return new int[]{Math.min(node.val, left[0]), Math.max(node.val, right[1]), left[2] + right[2] + 1}; }else{ return new int[]{Integer.MIN_VALUE, Integer.MAX_VALUE, Math.max(left[2], right[2])}; } } }
public class Solution { class Result { // (size, rangeLower, rangeUpper) -- size of current tree, range of current tree [rangeLower, rangeUpper] int size; int lower; int upper; Result(int size, int lower, int upper) { this.size = size; this.lower = lower; this.upper = upper; } } int max = 0; public int largestBSTSubtree(TreeNode root) { if (root == null) { return 0; } traverse(root); return max; } private Result traverse(TreeNode root) { if (root == null) { return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE); } Result left = traverse(root.left); Result right = traverse(root.right); if (left.size == -1 || right.size == -1 || root.val <= left.upper || root.val >= right.lower) { return new Result(-1, 0, 0); } int size = left.size + 1 + right.size; max = Math.max(size, max); return new Result(size, Math.min(left.lower, root.val), Math.max(right.upper, root.val)); } } /* in brute-force solution, we get information in a top-down manner. for O(n) solution, we do it in bottom-up manner, meaning we collect information during backtracking. */ public class Solution { class Result { // (size, rangeLower, rangeUpper) -- size of current tree, range of current tree [rangeLower, rangeUpper] int size; int lower; int upper; Result(int size, int lower, int upper) { this.size = size; this.lower = lower; this.upper = upper; } } int max = 0; public int largestBSTSubtree(TreeNode root) { if (root == null) { return 0; } traverse(root, null); return max; } private Result traverse(TreeNode root, TreeNode parent) { if (root == null) { return new Result(0, parent.val, parent.val); } Result left = traverse(root.left, root); Result right = traverse(root.right, root); if (left.size==-1 || right.size==-1 || root.val<left.upper || root.val>right.lower) { return new Result(-1, 0, 0); } int size = left.size + 1 + right.size; max = Math.max(size, max); return new Result(size, left.lower, right.upper); } }
posted on 2018-11-07 05:11 猪猪🐷 阅读(91) 评论(0) 收藏 举报
浙公网安备 33010602011771号