LeetCode刷题笔录 Validate Binary Search Tree
来源:互联网 发布:白金数据日语 编辑:程序博客网 时间:2024/05/21 21:34
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
上来先Brute force吧,没啥好说的。检查每个node的左子树的所有node都小于这个node,右子树的所有node都大于这个node。
public class Solution { public boolean isValidBST(TreeNode root) { if(root == null){ return true; } return isSubTreeLessThan(root.left, root.val) && isSubTreeGreaterThan(root.right, root.val) && isValidBST(root.left) && isValidBST(root.right); } public boolean isSubTreeLessThan(TreeNode node, int val){ if(node == null){ return true; } return node.val < val && isSubTreeLessThan(node.left, val) && isSubTreeLessThan(node.right, val); } public boolean isSubTreeGreaterThan(TreeNode node, int val){ if(node == null){ return true; } return node.val > val && isSubTreeGreaterThan(node.left, val) && isSubTreeGreaterThan(node.right, val); } }
public class Solution { public boolean isValidBST(TreeNode root) { return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE); } public boolean isBST(TreeNode node, int alpha, int beta){ if(node == null){ return true; } if(alpha < node.val && node.val < beta){ return isBST(node.left, alpha, node.val) && isBST(node.right, node.val, beta); } else return false; } }
还有一种方法:如果一棵树是BST,那么如果做一个in order traversal的话产生的数组应该是排好序的。这样就一边进行in order traversal,一边比较当前值是不是比前一个值大就行了。这里用了个static变量来记录之前的值,使其在递归时能被记住。如果用C++的话按引用传递就不需要static了。
public class Solution { public static int previous = Integer.MIN_VALUE; public boolean isValidBST(TreeNode root) { if(root == null) return true; //the left sub tree if(isValidBST(root.left) == false) return false; //the current node if(root.val <= previous) return false; previous = root.val; //the right subtree if(isValidBST(root.right) == false){ return false; } return true; }}
0 0
- LeetCode刷题笔录 Validate Binary Search Tree
- LeetCode刷题笔录Recover Binary Search Tree
- LeetCode刷题笔录Recover Binary Search Tree
- [leetcode刷题系列]Validate Binary Search Tree
- leetcode 刷题之路 29 Validate Binary Search Tree
- 开始刷题leetcode day37:Validate Binary Search Tree
- LeetCode: Validate Binary Search Tree
- [Leetcode] Validate Binary Search Tree
- LeetCode : Validate Binary Search Tree
- [LeetCode] Validate Binary Search Tree
- [LeetCode] Validate binary search tree
- [LeetCode]Validate Binary Search Tree
- [Leetcode]Validate Binary Search Tree
- [leetcode]Validate Binary Search Tree
- LeetCode-Validate Binary Search Tree
- [leetcode] Validate Binary Search Tree
- LeetCode - Validate Binary Search Tree
- [LeetCode] Validate Binary Search Tree
- 对程序员职业的一些建议
- 模拟器找不到sdcard,Environment.getExternalStorageState()=removed
- Wi ndows 7 字体显示不正常
- CSS中的any伪类
- 如何分辨打着科普旗号的伪科学谣言
- LeetCode刷题笔录 Validate Binary Search Tree
- cocos2dx 常见的动作详解(转载)
- Wooden Sticks
- [iOS] Check if app in iOS/Android, open link in app
- cocos2d-x 3.0 Android.mk 设置方法
- 不要省略CSS中零值的单位!
- MAVEN入门教程
- 我的Android SDK里面tools 的adb.exe 不见了
- 升级Spring,从2.5.5至3.2.8,过程实录