LeetCode 333. Largest BST Subtree(최대 두 갈래 검색 트리)

원본 사이트 주소:https://leetcode.com/problems/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. 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.
Hint:
You can recursively use algorithm similar to 98. Validate Binary Search Tree at each node of the tree, which will result in O(nlogn) time complexity.
Follow up: Can you figure out ways to solve it with O(n) time complexity?
방법: 귀속 검사.한 그루의 나무가 당겨지고 좌우의 나무만 BST이고 왼쪽 나무의 최대치는 뿌리 노드보다 작으며 오른쪽 나무의 최소치가 뿌리 노드보다 크면 이 나무가 BST이다.이 노드가 BST를 형성할 수 있는지, 그리고 BST의 노드 총수를 포함하여 귀속 함수의 반환 결과를 정한다.
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private int max = 0;
    private Range check(TreeNode root) {
        Range range = new Range(root.val, root.val);
        Range left = (root.left == null) ? null : check(root.left);
        Range right = (root.right == null) ? null: check(root.right);
        if (left != null) {
            if (!left.bst || left.max >= root.val) return range;
            range.min = left.min;
            range.count += left.count;
        }
        if (right != null) {
            if (!right.bst || root.val >= right.min) return range;
            range.max = right.max;
            range.count += right.count;
        }
        range.bst = true;
        max = Math.max(max, range.count);
        return range;
    }
    public int largestBSTSubtree(TreeNode root) {
        if (root != null) check(root);
        return max;
    }
}

class Range {
    boolean bst;
    int min, max;
    int count = 1;
    Range(int min, int max) {
        this.min = min;
        this.max = max;
    }
}

또 다른 구현:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    int max = 0;
    private Range find(TreeNode root) {
        Range range = new Range(root.val, root.val, 1);
        Range left = root.left == null ? null : find(root.left);
        Range right = root.right == null ? null : find(root.right);
        if (root.left != null) {
            if (left == null) return null;
            if (left.max >= root.val) return null;
            range.min = left.min;
            range.nodes += left.nodes;
        }
        if (root.right != null) {
            if (right == null) return null;
            if (right.min <= root.val) return null;
            range.max = right.max;
            range.nodes += right.nodes;
        }
        max = Math.max(max, range.nodes);
        return range;
    }
    public int largestBSTSubtree(TreeNode root) {
        if (root == null) return 0;
        find(root);
        return max;
    }
}
class Range {
    int min, max;
    int nodes;
    Range(int min, int max, int nodes) {
        this.min = min;
        this.max = max;
        this.nodes = nodes;
    }
}

좋은 웹페이지 즐겨찾기