Validate Binary Search Tree 解答
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 解答的更多相关文章
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 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 ...
- [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 ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- 瑞柏匡丞:app商业价值如何体现
在互联网行业,想要实现商业价值,必须先实现用户价值.这个观点发源自PC统治互联网的时代,如今PC端的用户停留时间下降,用户行为趋于稳定保守,移动端则蒸蒸日上.而PC与移动端的区别之一是,PC端的用户流 ...
- POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)
题意:给一个字符串S,判断在什么下标的时候,前缀和后缀相等,输出前缀和后缀相等的点. 分析:next数组的一种很巧妙的用法 next数组表示的意义是当前下标前面k字符和开头的前面k个字符相等 所以就会 ...
- 什么是内存泄漏?(What is a memory leak?)
程序中的内存泄漏是怎么回事呢? 我们写过很多带有关键词free()的程序.比如我在这篇博文关于链表的一些重要操作(Important operations on a Linked List)中删除整个 ...
- 相似文档查找算法之 simHash 简介及其 java 实现 - leejun_2005的个人页面 - 开源中国社区
相似文档查找算法之 simHash 简介及其 java 实现 - leejun_2005的个人页面 - 开源中国社区 相似文档查找算法之 simHash 简介及其 java 实现
- python3-day1(基础总结)
总结比较抽象的难点: 1.二进制运算 60 & 13 =12 60 | 13 =61 60 ^ 13 =49 60<<2 =240 60>>2 =15 ...
- 如何在编译内核时添加缺少的固件(随着intel wireless 5100 AGN的 iwlwifi 案例)
我不知道你在笔记本使用 Linux 在内核编译无线wifi 不能用.我的书"关联 Y450"一个足够的旧书,随着无线网卡: $ lspci | grep Wireless 06:0 ...
- [汇编学习笔记][第十章 CALL和RET指令]
第十章 CALL和RET指令 call和ret指令都是转移指令,它们都修改CS和IP.经常被共同用于实现子程序的设计.这一章,我们讲解call和ret指令的原理 10.1 ret和retf ret指令 ...
- [springmvc+mybatis][关于这两个框架的学习,我想说]
关于学习笔记 在对java web有了一定的了解后,这两个框架没怎么写学习笔记了…毕竟项目驱动型…… 关于学习资料 强烈推荐传智播客的燕青讲解的 让我对这种培训班教育的资料刮目相看(不过还是千万别去这 ...
- 关于document.write()重写页面
今天碰到了一个以前没注意的问题即:document.write(),在此拿来分享! document.write是最基本的JavaScript命令之一,这个命令简单地打印指定的文本内容到页面上(注意是 ...
- cookie那些事
本文面向对cookie有基本了解的读者,小白出门左转 设置cookie (HTTP 响应头) Set-Cookie: {name}={value};path={path};domain={doma ...