Validate Binary Search Tree(一定掌握的方法)
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.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
这题使用迭代的中序遍历。中序遍历是遍历左中右节点,所以对于二叉搜索树,后一个节点肯定比上一个节点大。所以根据这个思路,假设一个节点pre表示前一个节点,如果下一个节点比pre小,就返回false。中序遍历从最左边的子树开始遍历的,pre一开始就是null。当左中右遍历完,pre 在右子树上,这样继续上一层(中间的父节点)也是比pre大。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
if(root==null) return true;
Stack<TreeNode> stack=new Stack<>();
TreeNode pre=null;
while(root!=null||!stack.isEmpty()){
if(root!=null){ //将左节点依次存进栈,存完后开始遍历
stack.add(root);
root=root.left;
}else{
root=stack.pop();
if(pre!=null&&root.val<=pre.val) return false;
pre=root;
root=root.right;
}
}
return true;
}
}
下面是Binary Tree Inorder Traversal ,中序遍历二叉树非递归方法。
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.empty()){
if(root != null){
stack.push(root);
root = root.left;
}else{
root = stack.pop();
list.add(root.val);
root = root.right;
}
}
return list;
}
Validate Binary Search Tree(一定掌握的方法)的更多相关文章
- 【leetcode】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) ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 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) ...
- 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 dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- C语言--指针函数和函数指针
指针函数和函数指针 指针函数其实是一个简称,是指带指针的函数,它本质上是一个函数,只是返回的是某种类型的指针.其定义的格式为: 类型标识符 *函数名(参数表) 函数指针,从本质上说是一个指针,只是它 ...
- Simple tutorial for using TensorFlow to compute a linear regression
"""Simple tutorial for using TensorFlow to compute a linear regression. Parag K. Mita ...
- 03 EditText文本编辑框
二 EditText 文本编辑框 父类: TextView >概念:文本编辑框 可以进行文本编辑 android:textColor="#00f&qu ...
- 精通CSS+DIV网页样式与布局--页面和浏览器元素
在页面和浏览器中,除了文字.图片.表格.表单等,还有很多各种各样的元素,在上篇博文中,小编主要简单的介绍了一下在CSS中如何设置表格和表单,今天小编主要简单介绍一下丰富的超链接特效.鼠标特效.页面滚动 ...
- Xcode中Groups和Folder的区别
以前一直没有在意这个问题,今天"中奖"了 ;( 在Xcode文件夹视图中会发现2种颜色的文件夹图标:黄色和蓝色. 黄色表示组(groups),蓝色表示文件夹(folder). 组只 ...
- SharePoint 2013: The "New Web Application" button is disabled is the central administration
安装完sharepoint foundation2013后,直接通过url访问管理中心,想进application management去新建的话会发现新建按钮是灰色的,即使你用的是系统管理员账号并且 ...
- maven settings 配置文件
maven settings 配置文件 <?xml version="1.0" encoding="UTF-8"?> <settings xm ...
- GIT版本控制 — GIT与SVN的相互转换 (三)
git-svn git-svn用于Git和SVN的转换,可以把Git仓库迁移成SVN仓库,反之亦可. 详细介绍可见[1],或者命令行输入git-svn. Bidirectional operation ...
- Dynamics CRM 同一实体多个Form显示不同的Ribbon按钮
自CRM2011引入多FORM窗体,并且对不同的窗体引入了角色控制,给我们的客制化开发带来了多样化,既然有了多窗体也就理所当然的有了在不同的窗体显示不同的Ribbon按钮的需求,具体怎么做见下面的博客 ...
- (NO.00001)iOS游戏SpeedBoy Lite成形记(十八)
现在需要实现具体的游戏逻辑大致如下: 玩家点击某条赛道选择一个选手,然后会弹出菜单窗口让玩家输入压赌的金额,如果输入的金额值非法,则在GameInterface下部的状态栏中显示提示,要求玩家重新输入 ...