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   3
Binary 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;}
Recursive:

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
原创粉丝点击