98. Validate Binary Search Tree
来源:互联网 发布:java从笨鸟到菜鸟 编辑:程序博客网 时间:2024/06/05 09:23
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 3Binary tree
[2,1,3]
, return true.Example 2:
1 / \ 2 3Binary tree
[1,2,3]
, return false.参照中序遍历模板JAVA 二叉树遍历,增加一个prev的节点。代码如下:
/** * 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) { Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode prev = null; while (root != null || stack.size() > 0) { while (root != null) { stack.push(root); root = root.left; } root = stack.pop(); if (prev != null && prev.val >= root.val) { return false; } prev = root; root = root.right; } return true; }}
阅读全文
0 0
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 2016 was a landmark year for big data
- Srping Boot 注解整理
- 爬虫-爬取网站上的图片
- 单点登录的三种实现方式
- 微信签名及jsSDK的使用
- 98. Validate Binary Search Tree
- [李景山php] XSS攻击
- java中set集合的使用方法
- 占位:第一章
- 05 WebGL 着色器编程语言GLSL ES的结构体
- 搜索 O题
- 在一个JSP页面中包含另一个JSP页面的三种方式
- 浅析线程的同步与互斥机制
- gradle 修改apk 名称