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 }
  • 相关阅读:
    视频流媒体服务器网络硬盘录像机NVR接入/解码/转发能力解析
    流媒体服务器安装失败/程序启动错误等问题解决方案
    监控摄像头如何用作网络直播?
    数据库之单表查询
    数据库之表与表之间的关系
    数据库之完整性约束
    数据库之数据类型
    数据库之增删改查操作
    数据库之基本操作和存储引擎
    数据库之数据库基础及安装
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5077532.html
Copyright © 2011-2022 走看看