• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

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 }

 

posted @ 2015-12-26 08:49  neverlandly  阅读(392)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3