zoukankan      html  css  js  c++  java
  • Leetcode: Inorder Successor in BST

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
    
    Note: If the given node has no in-order successor in the tree, return null.

    网上看到更好的方法:https://leetcode.com/discuss/77805/java-5ms-short-code-with-explanations 

    The idea is to compare root's value with p's value if root is not null, and consider the following two cases:

    • root.val > p.val. In this case, root can be a possible answer, so we store the root node first and call it res. However, we don't know if there is anymore node on root's left that is larger than p.val. So we move root to its left and check again.

    • root.val <= p.val. In this case, root cannot be p's inorder successor, neither canroot's left child. So we only need to consider root's right child, thus we move root to its right and check again.

    We continuously move root until exhausted. To this point, we only need to return the res in case 1.

     1 public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
     2     TreeNode res = null;
     3     while(root!=null) {
     4         if(root.val > p.val) {
     5             res = root;
     6             root = root.left;
     7         }
     8         else root = root.right;
     9     }
    10     return res;
    11 }

    算Predecessor in BST也是类似的

     1 public TreeNode inorderPrecessor(TreeNode root, TreeNode p) {
     2     TreeNode res = null;
     3     while (root != null) {
     4         if (root.val < p.val) {
     5             //root could be p's predecessor, but we don't know if there's one in root.right 
     6             //that is even closer to p. 
     7             res = root;
     8             root = root.right;
     9         }
    10         else { //root could not be p's predecessor
    11             root = root.left;
    12         }
    13     }
    14     return res;
    15 }

    Recursion: use Inorder traversal

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
    12         ArrayList<TreeNode> prev = new ArrayList<TreeNode>();
    13         ArrayList<TreeNode> res = new ArrayList<TreeNode>();
    14         prev.add(null);
    15         res.add(null);
    16         inorder(root, p, prev, res);
    17         return res.get(0);
    18     }
    19     
    20     public void inorder(TreeNode root, TreeNode p, ArrayList<TreeNode> prev, ArrayList<TreeNode> res) {
    21         if (root == null) return;
    22         inorder(root.left, p, prev, res);
    23         if (prev.get(0) == p) {
    24             res.set(0, root);
    25         }
    26         prev.set(0, root);
    27         inorder(root.right, p, prev, res);
    28     }
    29 }
  • 相关阅读:
    搜索复习-中级水题
    搜索复习-基础水题(一共12道)
    TCPThree_C杯 Day1
    bzoj1579 道路升级
    bzoj3732 Network(NOIP2013 货车运输)
    bzoj1624 寻宝之路
    bzoj1430 小猴打架
    bzoj2763 飞行路线
    2017-10-28-afternoon-清北模拟赛
    2017-10-28-morning-清北模拟赛
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5077532.html
Copyright © 2011-2022 走看看