JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
复制代码
 1 public class Solution {
 2     public boolean isValidBST(TreeNode root) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         if(root == null)
 6             return true;
 7         if(root.left != null)
 8         {
 9             TreeNode left = root.left;
10             while(left.right != null)
11                 left = left.right;
12             if(root.val <= left.val)
13                 return false; 
14         }
15         if(root.right != null)
16         {
17             TreeNode right = root.right;
18             while(right.left != null)
19                 right = right.left;
20             if(root.val >= right.val)
21                 return false;
22         }
23         return isValidBST(root.left) && isValidBST(root.right);
24     }
25 }
复制代码

 

posted on   JasonChang  阅读(176)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示