• 98_验证二叉搜索树


    98_验证二叉搜索树 

    package 二叉树.二叉搜索树;
    
    import java.util.ArrayList;
    import java.util.Deque;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * https://leetcode-cn.com/problems/validate-binary-search-tree/
     * 
     * @author Huangyujun
     *
     *       
     */
    public class _98_验证二叉搜索树 {
        // (错误) 错误原因:这个代码只能保证局部父子节点关系为 左<中<右, 不能保证整棵树的左边所有节点都比根节点小,右边同理。
    //    public boolean isValidBST(TreeNode root) {
    //        if (root == null)
    //            return false;
    //        if (root.left == null && root.right == null) {
    //            return true;
    //        } else if (root.left == null) {
    //            if (root.right.val <= root.val) {
    //                return false;
    //            }
    //        } else if (root.right == null) {
    //            if (root.left.val >= root.val) {
    //                return false;
    //            }
    //        }
    //        // 当前结点判断
    //        return isValidBST(root.left) && isValidBST(root.right);
    //    }
        
        /**
         * 官网是通过重载接口方法使用递归法,(参数:根,最小,最大)~ 实现了局部到整体都满足:
         * 二叉搜索树:根大于左子树,大于左边整棵树的最大值哦,同理,根小于右子树,小于右边整棵树的最小值。
         */
        class Solution {
            public boolean isValidBST(TreeNode root) {
                return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
            }
    
            public boolean isValidBST(TreeNode node, long lower, long upper) {
                if (node == null) {
                    return true;
                }
                if (node.val <= lower || node.val >= upper) {
                    return false;
                }
                return isValidBST(node.left, lower, node.val) && isValidBST(node.right, node.val, upper);
            }
        }
        //可以,但是不够优秀:
        //class Solution {
    //        List<Integer> list = new ArrayList<>();
    //        public boolean isValidBST(TreeNode root) {
    //            if(root == null)     return true;
    //            //思路二:中序拿到的元素(是否实现了从小到大)
    //            int size = list.size();
    //            for(int i = 0; i < size - 1; i++) {//验证是从小到大
    //                if(list.get(i) >= list.get(i + 1)) {
    //                    return false;
    //                }
    //            }
    //            return true;
    //        }
    //        public void inorder(TreeNode root) {
    //            if(root == null)    return;
    //            inorder(root.left);
    //            list.add(root.val);
    //            inorder(root.right);
    //        }
        //}
    
    
        /**
         * 官网遍厉二叉树的结点时,迭代写法:
         while (!stack.isEmpty() || node != null) {
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
            // 即根
            node = stack.pop();
            。。。
         }             
         *  我的: (我的错误之处,在于开始: node = stack.pop(); 后面:node = stack.pop(); 这样写其实pop() 了两次)
                stack.push(root);
                while(!stack.isEmpty() ) {    
                    node = stack.pop();
                    while(node != null ) {    
                        node = node.left;
                        stack.push(node);
                    }
                    // 即根
                    node = stack.pop();
                    。。。
                }    
         */
        //中序遍历(迭代法/递归法)~ 从小到大啦(用一个变量记录前一次“第一次”,当前本次(第二次)与之对比)
        //这里的记录变量是:inorder
        class Solution2 {
            public boolean isValidBST(TreeNode root) {
                Deque<TreeNode> stack = new LinkedList<TreeNode>();
                double inorder = -Double.MAX_VALUE;
                TreeNode node = root;
                while (!stack.isEmpty() || node != null) {
                    while (node != null) {
                        stack.push(node);
                        node = node.left;
                    }
                    // 即根
                    node = stack.pop();
                    // 如果中序遍历得到的节点的值小于等于前一个 inorder,说明不是二叉搜索树
                    if (node.val <= inorder) {
                        return false;
                    }
                    inorder = node.val;
                    // 判断该点
                    node = node.right;
                }
                return true;
            }
        }
        
        
    }

    本文来自博客园,作者:一乐乐,转载请注明原文链接:https://www.cnblogs.com/shan333/p/15709293.html

  • 相关阅读:
    简单读取winfrom资源文件
    string.Format对C#字符串格式化
    如何在SQL中使用循环结构
    Linq to SQL 多条件动态组合查询(实战篇)
    提问的艺术
    XtraGrid 单元格加边框颜色
    凭证控件制作
    C# double 四舍五入
    自定义光标样式
    触发窗体事件(例如按Esc关闭窗体)
  • 原文地址:https://www.cnblogs.com/shan333/p/15709293.html
Copyright © 2020-2023  润新知