LeetCode-探索-初级算法-树-2. 验证二叉搜索树(个人做题记录,不是习题讲解)

LeetCode-探索-初级算法-树-2. 验证二叉搜索树(个人做题记录,不是习题讲解)

LeetCode探索-初级算法:https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/

  1. 验证二叉搜索树
  • 语言:java

  • 思路:本来想通过求左右子树是否是二叉搜索树,顺便求左右子树的最大最小值,但是应该是代码写得有问题,遇到极端条件某些树的值是Integer.MAX_VALUE或者Integer.MIN_VALUE就得不到正确答案。

  • 参考代码1(1ms):先判断左子树是否存在大于根节点的子节点(左子树的往下遍历所有右节点所有都必须小于根节点),再判断右子树是否存在小于根节点的子节点(右子树的往下遍历的所有左节点都必须小于根节点),然后以递归形式判断左右子树是否和根节点一样符合要求(最后的return递归函数)

    https://www.cnblogs.com/Xieyang-blog/p/9111962.html

    public boolean isValidBST(TreeNode root) {
            if (root == null) {
                return true;
            }
            
            if (root.left != null) {
                TreeNode cur = root.left;
                while (cur.right != null) {
                    cur = cur.right;
                }
                if (cur.val >= root.val) {
                    return false;
                }
            }
            
            if (root.right != null) {
                TreeNode cur = root.right;
                while (cur.left != null) {
                    cur = cur.left;
                }
                if (cur.val <= root.val) {
                    return false;
                }
            }
            
            return isValidBST(root.left) && isValidBST(root.right);
        }
    
  • 参考代码2(0ms):这个利用投机取巧的方式,最大最小值策略,但是如果题目也是用Long的范围,那么root就是Long.MAX_VALUE时就行不通了,所以其实我个人不喜欢这个解题方式(太过针对Integer的范围,适用性差)

    https://www.cnblogs.com/Xieyang-blog/p/9111962.html

    class Solution {
        public boolean isValidBST(TreeNode root) {
            return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
        }
        
        public boolean isValidBST(TreeNode root, long minVal, long maxVal) {
            if (root == null) return true;
            if (root.val >= maxVal || root.val <= minVal) return false;
            return isValidBST(root.left, minVal, root.val) && isValidBST(root.right, root.val, maxVal);
        }
    }
    
  • 参考代码3(4ms):利用中序遍历的左中右顺序刚好是左<中<右时,即满足二叉搜索树条件来判断

    https://www.cnblogs.com/Xieyang-blog/p/9111962.html

    class Solution {
        public boolean isValidBST(TreeNode root) {
       if (root == null) return true;
       Stack<TreeNode> stack = new Stack<>();
       TreeNode pre = null;
       while (root != null || !stack.isEmpty()) {
          while (root != null) {
             stack.push(root);
             root = root.left;
          }
          root = stack.pop();
          if(pre != null && root.val <= pre.val) return false;
          pre = root;
          root = root.right;
       }
       return true;
    }
    }
    
  • 参考代码4(0ms):利用类变量记录根节点,其实也是通过中序遍历树的思想判断是否是左<中<右。

    class Solution {
        TreeNode pre = null;
        public boolean isValidBST(TreeNode root) {
            if(root==null) {
    			return true;
    		}
    		
    		if(!isValidBST(root.left))  return false;
    		
    		if(pre!=null && pre.val>=root.val) {
    			return false;
    		}
    		pre=root;
            return isValidBST(root.right);
        }
    }
    
  • 参考4后重写:中序遍历判断是否存在遍历的下一节点大于上一节点的情况,存在则说明不是二叉搜索树(遍历顺序:左中右)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        TreeNode pre = null;
        public boolean isValidBST(TreeNode root) {
            if(root == null)
                return true;
            if(!isValidBST(root.left))
                return false;
            if(pre!=null&&pre.val>=root.val)
                return false;
            pre = root;
            return isValidBST(root.right);
        }
    }
    
发布了60 篇原创文章 · 获赞 6 · 访问量 5537

猜你喜欢

转载自blog.csdn.net/Ashiamd/article/details/102263355