Question

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.

Solution 1 -- Recursive

According to the question, we can write recursive statements. Note here whole left/right subtree should be smaller/greater than the root.

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null)
return true;
if (root.left != null && !smallerThanRoot(root, root.left))
return false;
if (root.right != null && !greaterThanRoot(root, root.right))
return false;
if (isValidBST(root.left) && isValidBST(root.right))
return true;
return false;
} private boolean greaterThanRoot(TreeNode root, TreeNode child) {
if (child.val <= root.val)
return false;
if (child.left != null) {
if (!greaterThanRoot(root, child.left))
return false;
}
if (child.right != null) {
if (!greaterThanRoot(root, child.right))
return false;
}
return true;
} private boolean smallerThanRoot(TreeNode root, TreeNode child) {
if (child.val >= root.val)
return false;
if (child.left != null) {
if (!smallerThanRoot(root, child.left))
return false;
}
if (child.right != null) {
if (!smallerThanRoot(root, child.right))
return false;
}
return true;
}
}

Solution 2 -- Inorder Traversal

Inorder traversal of BST is an ascending array. Java Stack

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
// This problem can be looked as inorder traversal problem
// Inorder traversal of BST is an ascending array
List<Integer> inOrderResult = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode tmp = root;
while (tmp != null || !stack.empty()) {
if (tmp != null) {
stack.push(tmp);
tmp = tmp.left;
} else {
TreeNode current = stack.pop();
inOrderResult.add(current.val);
tmp = current.right;
}
}
// Traverse list
if (inOrderResult.size() < 1)
return true;
int max = inOrderResult.get(0);
for (int i = 1; i < inOrderResult.size(); i++) {
if (inOrderResult.get(i) > max)
max = inOrderResult.get(i);
else
return false;
}
return true;
}
}

Validate Binary Search Tree 解答的更多相关文章

  1. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  2. Validate Binary Search Tree

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

  3. 【leetcode】Validate Binary Search Tree

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

  4. LintCode Validate Binary Search Tree

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

  5. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

  6. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

  7. 【LeetCode练习题】Validate Binary Search Tree

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

  8. leetcode dfs Validate Binary Search Tree

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

  9. LeetCode: Validate Binary Search Tree 解题报告

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

随机推荐

  1. cf446C DZY Loves Fibonacci Numbers

    C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...

  2. Duff策略

    Tom Duff首先在C语言中提出了展开循环的构想,所以这种模式被称之为Duff策略.Duff策略背后的思想是每一次循环完成标准循环的1-8次.首先通过数组值得总数除以8来取定循环次数.Duff发现对 ...

  3. Nx32926 命令关机

    一. poweroff关机命令 ~ # poweroff ~ # baud=, quot= w-config: 8bits/char umount: devtmpfs busy - remounted ...

  4. pyqt 同时勾选多个items(网友提供学习)

    框选多个item之后,用空格键可以勾选/去选多个item,效果如下图所示: http://oglop.gitbooks.io/pyqt-pyside-cookbook/list/img/checkbo ...

  5. PPT去掉图片白色背景

    双击图片,点击菜单栏“删除背景”,用矩形框选中想要的区域,然后将鼠标焦点移到图片外,单击鼠标即可.

  6. 写PPT的方法

    这个方法是今天同事的方法,看到他的PPT简洁高效,明了,记下了他的方法: 写文字:写框架,这个框架或者内容可以是word形式的,目的是展示内容 找模板:在搜集到的各种ppt模板中选几个适合自己文字的页 ...

  7. 动态载入TreeView时让TreeView节点前显示加号

    解释下标题,我这里通过webservice获取数据并动态载入TreeView节点.那么某个节点展开前它是没有子节点的.那么它就不显示加号.这样会让用户误以为此节点不能展开.我是这样做的,每次创建节点a ...

  8. 【COCOS2DX-对28游戏开发】 Cocos2d-x-3c 道路设计 CocosBase CocosNet CocosWidget

    原文链接:http://blog.csdn.net/cocosviva/article/details/18970717 另一个比較不错的cocos2dx扩展库:https://github.com/ ...

  9. Intellij Idea安装主题

    IDEA中jar包形式的主题比较常见.(顺便给大家推荐一个主题站:http://www.ideacolorthemes.org/themes/) 从主菜单中依次选择[File]>[Import ...

  10. 关于arm-linux-gcc的安装与配置

    在嵌入式开发中我们经常会用到arm-linux-gcc来编译我们的应用程序.作为arm-linux-gcc的入门,我们先看看如何安装arm-linux-gcc. 安装arm-linux-gcc还是比较 ...