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.

解题思路:

本题方法多多,可以采用DFS,当然最简答的方法是采用之前的中序遍历Java for LeetCode 094 Binary Tree Inorder Traversal检查得到的List是否有序即可。

    public boolean isValidBST(TreeNode root) {

        List<Integer> list=inorderTraversal(root);
if(list.size()==0)
return true;
int temp=list.get(0);
for(int i=1;i<list.size();i++){
if(temp>=list.get(i))
return false;
temp=list.get(i);
}
return true;
}
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
if (root.left != null)
list.addAll(inorderTraversal(root.left));
list.add(root.val);
if (root.right != null)
list.addAll(inorderTraversal(root.right));
return list;
}

JAVA实现如下:

Java for LeetCode 098 Validate Binary Search Tree的更多相关文章

  1. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  2. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  3. [LeetCode]题解(python):098 Validate Binary Search Tree

    题目来源 https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it ...

  4. leetcode 98 Validate Binary Search Tree ----- java

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. Java for LeetCode 099 Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. Leetcode 98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. 【题解】【BST】【Leetcode】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. wpa破解学习

    TENDA  159031106A iPhone 192.168.0.11 90:27:E4:53:49:D6 18:58:52 PC-201211262044 192.168.0.12 00:F1: ...

  2. 【GLSL教程】(二)在OpenGL中使用GLSL 【转】

    http://blog.csdn.net/racehorse/article/details/6616256 设置GLSL 这一节讲述在OpenGL中配置GLSL,假设你已经写好了顶点shader和像 ...

  3. z pre-pass 相关问题的讨论

    z pre-pass 是指在渲染流程中,第一个pass先画一张深度buffer出来,得到需要绘制的最前面这层深度,用这个在接下来的pass中做深度剔出,这样在第二个pass中会省略很多绘制. 这项技术 ...

  4. Ambient Occulution

    SSAO HDAO normal pair 求一个谷 SAO 重建normal HBAO input depth,normal 这几个都是screen space的ao

  5. node.js(一)- 安装配置

    最近在学习node,文章作为记录 一.下载 直接下载最新的包:https://nodejs.org/en/download/ 我这里是自己做开发,所以直接使用的是window 64位的最新v4.5.0 ...

  6. JAVA Eclipse 创建android xml看不到预览怎么办

    电机安卓图标,设置为更低的API版本即可

  7. JAVA_Could not find property [struts.actionMapping]怎么办

    你的项目中不包含log4j.jar这个文件,包含进去即可

  8. linux 挂载移动盘

    http://www.2cto.com/os/201411/354319.html 磁盘出现问题,有时候卸载不掉 参见http://blog.csdn.net/davil_dev/article/de ...

  9. 入门--JTBC系统学习(1)

    下载代码 JTBC有还例如以下几类 JDK(1.6)+JSP(2.0)+MYSQL/SQLITE ASP.NET(2.0)+ACCESS/SQL SERVER PHP+MYSQL ASP(3.0)+A ...

  10. 设置windows时间开机同步方法

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 适用场景: 主板电池 ...