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 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值