/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{
classResult{ TreeNode node; // BST根节点 int size; // BST的size int max; // BST的最大值 int min; // BST的最小值 }
publicintlargestBSTSubtree(TreeNode root){ Result r = visit(root); return r == null ? 0 : r.size; }
public Result visit(TreeNode node){ if (node == null) returnnull;
Result l = null, r = null; if (node.left != null) l = visit(node.left); if (node.right != null) r = visit(node.right);