98. Validate Binary Search Tree**
来源:互联网 发布:淘宝商家贷款 编辑:程序博客网 时间:2024/04/29 00:49
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 3
Binary tree [1,2,3]
, return false.
Learn one iterative inorder traversal, apply it to multiple tree questions (Java Solution)
I will show you all how to tackle various tree questions using iterative inorder traversal. First one is the standard iterative inorder traversal using stack. Hope everyone agrees with this solution.
Question : 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()){ while(root != null){ stack.push(root); root = root.left; } root = stack.pop(); list.add(root.val); root = root.right; } return list;}
Now, we can use this structure to find the Kth smallest element in BST.
Question : Kth Smallest Element in a BST
public int kthSmallest(TreeNode root, int k) { Stack<TreeNode> stack = new Stack<>(); while(root != null || !stack.isEmpty()) { while(root != null) { stack.push(root); root = root.left; } root = stack.pop(); if(--k == 0) break; root = root.right; } return root.val; }
We can also use this structure to solve BST validation question.
Question : Validate Binary Search Tree
public boolean isValidBST(TreeNode root) { if (root == null) return true; Stack<TreeNode> stack = new Stack<>(); TreeNode pre = null; while (root != null || !stack.isEmpty()) { while (root != null) { stack.push(root); root = root.left; } root = stack.pop(); if(pre != null && root.val <= pre.val) return false; pre = root; root = root.right; } return true;}
public class Solution { public boolean isValidBST(TreeNode root) { return isValidBST(root,Long.MIN_VALUE,Long.MAX_VALUE); } public boolean isValidBST(TreeNode root, long minval, long maxval){ if(root==null) return true; if(root.val>=maxval||root.val<=minval) return false; return isValidBST(root.left,minval,root.val)&&isValidBST(root.right, root.val,maxval); }}
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
- HttpClient 4.5版本设置连接超时时间
- DFS-深度优先搜索—2
- php 处理 非法访问
- Python编程的中文问题
- Zookeeper + ActiveMQ 集群整合
- 98. Validate Binary Search Tree**
- NOIP 2012 提高组 复赛 day1 drive 开车旅行
- SharedSDK(一):Android SharedSDK使用步骤
- 单词搜索
- 12 Iterationg Over Data With JSX
- 字串统计
- BurpSuite系列(十)----Extender模块(扩展器)
- Android 工具类 -->文件压缩
- 通过SwipeRefreshLayout实现ListView的上拉加载下拉刷新