leetCode 98:Validate Binary Search Tree

转自:http://blog.csdn.net/ljiabin/article/details/41699241点击打开链接

【题目】

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.
【解析】

题意:判断一个二叉树是否为二分查找树。

何为二分查找树?1) 左子树的值都比根节点小;2) 右子树的值都比根节点大;3) 左右子树也必须满足上面两个条件。

需要注意的是,左子树的所有节点都要比根节点小,而非只是其左孩子比其小,右子树同样。这是很容易出错的一点是,很多人往往只考虑了每个根节点比其左孩子大比其右孩子小。如下面非二分查找树,如果只比较节点和其左右孩子的关系大小,它是满足的。

     5
  /     \
4      10
      /      \
    3        11

【错误代码示范】【NA】

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Definition for binary tree 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public boolean isValidBST(TreeNode root) {  
  12.         if (root == nullreturn true;  
  13.         if (root.left != null && root.val <= root.left.val) return false;  
  14.         if (root.right != null && root.val >= root.right.val) return false;  
  15.         return isValidBST(root.left) && isValidBST(root.right);  
  16.     }  
  17. }  


【暴力遍历法】【AC】

从根节点开始递归,遍历所有的节点。并且在每个节点处,分别遍历其左右子树,判断其左子树的最大值比其小,右子树的最小值比其大。

时间复杂度为O(n^2)。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Solution {  
  2.     public boolean isValidBST(TreeNode root) {  
  3.         if (root == nullreturn true;  
  4.         if (!dfsLeft(root.left, root.val) || !dfsRight(root.right, root.val)) return false;  
  5.         return isValidBST(root.left) && isValidBST(root.right);  
  6.     }  
  7.       
  8.     public boolean dfsLeft(TreeNode root, int value) {  
  9.         if (root == nullreturn true;  
  10.         if (root.val >= value) return false;  
  11.         return dfsLeft(root.left, value) && dfsLeft(root.right, value);  
  12.     }  
  13.       
  14.     public boolean dfsRight(TreeNode root, int value) {  
  15.         if (root == nullreturn true;  
  16.         if (root.val <= value) return false;  
  17.         return dfsRight(root.left, value) && dfsRight(root.right, value);  
  18.     }  
  19. }  


【失效O(n)解法:最大最小值法】【NA】

网上很用人用了Integer.MIN_VALUE和Integer.MAX_VALUE来辅助解决这道题,即遍历时记录一个当前允许的最大值和最小值。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Solution {  
  2.     public boolean isValidBST(TreeNode root) {  
  3.         if (root == nullreturn true;  
  4.         if (root.left == null && root.right == nullreturn true;  
  5.         return validate(root, Integer.MIN_VALUE, Integer.MAX_VALUE);  
  6.     }  
  7.       
  8.     public boolean validate(TreeNode root, int min, int max) {  
  9.         if (root == nullreturn true;  
  10.         if (root.val <= min || root.val >= max) return false;  
  11.         return validate(root.left, min, root.val) && validate(root.right, root.val, max);  
  12.     }  
  13. }  

但是现在的LeetCode已经更新了这道题,下面这种解法已经通不过了,因为LeetCode多了两个测试用例:

Input: {-2147483648,#,2147483647}
Output: false
Expected: true


【正确O(n)解法:中序遍历法】【AC】【推荐】

二分查找树的中序遍历结果是一个递增序列。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Solution {  
  2.     List<Integer> list = new ArrayList<Integer>();  
  3.       
  4.     public boolean isValidBST(TreeNode root) {  
  5.         if (root == nullreturn true;  
  6.         if (root.left == null && root.right == nullreturn true;  
  7.         inOrderTraversal(root);  
  8.         for (int i = 1; i < list.size(); i++) {  
  9.             if (list.get(i) <= list.get(i - 1)) return false;  
  10.         }  
  11.         return true;  
  12.      }  
  13.       
  14.     public void inOrderTraversal(TreeNode root) {  
  15.         if (root == nullreturn;  
  16.         inOrderTraversal(root.left);  
  17.         list.add(root.val);  
  18.         inOrderTraversal(root.right);  
  19.     }  
  20. }  

比较奇怪的是,这种方法和上面的暴力遍历方法在LeetCode上消耗的时间都是500ms。


【中序遍历法升级版】

看到网上一个比较高级的中序遍历写法,不需要额外的O(n)的空间,而且通过的时间为436ms。一起来学习一下。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Solution {  
  2.     // Keep the previous value in inorder traversal.  
  3.     TreeNode pre = null;  
  4.       
  5.     public boolean isValidBST(TreeNode root) {  
  6.         // Traverse the tree in inorder.  
  7.         if (root != null) {  
  8.             // Inorder traversal: left first.  
  9.             if (!isValidBST(root.left)) return false;  
  10.               
  11.             // Compare it with the previous value in inorder traversal.  
  12.             if (pre != null && root.val <= pre.val) return false;  
  13.               
  14.             // Update the previous value.  
  15.             pre = root;  
  16.               
  17.             // Inorder traversal: right last.  
  18.             return isValidBST(root.right);  
  19.         }  
  20.         return true;  
  21.      }  
  22. }  

需要注意的是,TreeNode pre = null; 一定要在方法外边声明,原博客是写在方法里面的是不对的,因为写在里面的话,每次递归都是不同的pre。


参考来源:

http://huntfor.iteye.com/blog/2070278

http://www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值