[leetcode] Validate Binary Search Tree dfs
来源:互联网 发布:cos圈 知乎 编辑:程序博客网 时间:2024/05/17 07:41
方法1: inorder traversal, 如果in order就是valid (用int[] 做publicly accessible place,就不能过min_value, 这样的话就要用arraylist,然后放null)
方法2: 按definition构建recursion, left: helper(root.left, min, root.val);right: helper(root.right, root.val, max)
1.
public boolean isValidBST(TreeNode root) { ArrayList<Integer> pre = new ArrayList<Integer>(); pre.add(null); return helper(root, pre); } private boolean helper(TreeNode root, List<Integer> pre) { if (root==null) return true; boolean left = helper(root.left, pre); if (!left) return false; if (pre.get(0)!=null && root.val<=pre.get(0)) return false; pre.set(0, root.val); return helper(root.right, pre); }
2: ( 过不了oj,因为有min_value test case)
public boolean isValidBST(TreeNode root) { return helper(root, Integer.MIN_VALUE, Integer.MAX_VALUE); }boolean helper(TreeNode root, int min, int max) { if(root == null) return true; if(root.val <= min || root.val >= max) return false; return helper(root.left, min, root.val) && helper(root.right, root.val, max);}
0 0
- leetcode dfs Validate Binary Search Tree
- [leetcode] Validate Binary Search Tree dfs
- DFS Validate Binary Search Tree
- LeetCode-98-Validate Binary Search Tree dfs二叉树
- 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
- Leetcode Validate Binary Search Tree
- LeetCode | Validate Binary Search Tree
- EF 从PD到数据库再到Model所经过的二三步
- nginx下 编译安装php
- 【HNOI2004】宠物收养所【重做Splay做法】
- 标准C基础知识六
- php Ajax乱码解决方法
- [leetcode] Validate Binary Search Tree dfs
- 标准C基础知识笔记七
- 自动化杂谈之我们应该以怎样的过程学习自动化
- AndroidManifest.xml文件详解(uses-feature)
- PHP中如何判断AJAX提交的数据
- 排序
- 如何从屏幕边界来 检测手势
- 理解“统一编址与独立编址、I/O端口与I/O内存”
- Ansible学习之配置(3)