leetcode 333. Largest BST Subtree
来源:互联网 发布:程序员为什么容易猝死 编辑:程序博客网 时间:2024/05/20 05:58
leetcode 333. Largest BST Subtree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int largestBSTSubtree(TreeNode root) { if(root==null) return 0; if(root.left==null&&root.right==null) return 1; if(isValid(root, null, null)) return countNode(root); return Math.max(largestBSTSubtree(root.left),largestBSTSubtree(root.right)); } private boolean isValid(TreeNode root, Integer min, Integer max){ if(root==null) return true; if(min!=null&&min>=root.val) return false; if(max!=null&&max<=root.val) return false; return isValid(root.left, min, root.val)&&isValid(root.right,root.val,max); } private int countNode(TreeNode root){ if(root==null) return 0; if(root.left==null&&root.right==null) return 1; return 1 + countNode(root.left) + countNode(root.right); }}
阅读全文
0 0
- LeetCode 333. Largest BST Subtree
- LeetCode 333. Largest BST Subtree
- leetcode 333. Largest BST Subtree
- [leetcode] 333. Largest BST Subtree 解题报告
- Leetcode 333. Largest BST Subtree (Medium) (cpp)
- [Leetcode] 333. Largest BST Subtree 解题报告
- 333. Largest BST Subtree
- 333. Largest BST Subtree
- 333. Largest BST Subtree
- LeetCode 333. Largest BST Subtree(最大二叉搜索树)
- Largest BST Subtree
- Largest BST Subtree
- Medium 333题 Largest BST Subtree
- Largest Subtree Which is a Binary Search Tree (BST)
- 【Lintcode|leetcode】245 Subtree
- LeetCode: Subtree of Another Tree
- [LeetCode] Most Frequent Subtree Sum
- leetcode : subtree of another tree
- nrpe安装
- Codeforces869B The Eternal Immortality
- new运算符做了什么
- 使用VUE-CLI构建一个项目基本的配置
- Python之dict的妙用
- leetcode 333. Largest BST Subtree
- SSM中解决中文乱码问题
- 使用libpcap库过滤pcap文件中的数据包
- bzoj2369 && 2687 -- 决策单调性优化DP
- NKOJ-Unknow 死亡的颂唱者
- mongodb批量删除数据
- mooc 1-3
- Linux实验报告一
- 欢迎使用CSDN-markdown编辑器