285. Inorder Successor in BST

题目本身:https://leetcode.com/problems/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.


 

11/02/16 update: 底下写的有问题,也不好看

如果没有parent node:

1. iterative写法:

如果当前节点的值>目标节点的值,也就是说目标节点在左子树上,需要更新succ的值:

  更新suc

  当前节点左沉

否则,当前节点等于目标节点<=目标节点的值,不需要更新succ的值

  当前节点右沉

代码:

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

每次都会下沉,所以是树的高度的时间复杂度,O(logn)

 

2. recursive

inorderSuccessor(TreeNode root, TreeNode p)

如果root == null

  返回null

如果p.val > root.val,就是说p在root的右子树上

  返回inorderSuccessor(root.right, p)
否则,p在root的左子树上,也就是说,可能的结果是当前节点,或者下面有更接近p的值的那个节点

  leftRes = inorderSuccessor(root.left, p);
  如果leftRes找到了结果,就返回那个,否则就返回但该案节点

代码:

 1     public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
 2         if(root == null) {
 3             return null;
 4         }   
 5         if(root.val > p.val) {//p is in the left subtree
 6             TreeNode left = inorderSuccessor(root.left, p);
 7             return left == null? root: left;
 8         } else {
 9             return inorderSuccessor(root.right, p);
10         }
11     }

O(logn)

 


 原贴:

一定一定要记住tree inorder traversal,很多变种题!

目标:找到第一个比node.val值大的点

分析:

  1.如果node有右子树,那么答案就是右子树的最左下方节点

  2.如果这个node在它的父节点的右子树上,那么就没有答案了。因为:

    1)这个树没有右子树,而它的左子树的点值都比它小。

    2)这个树的父节点的左子树(它的左sibling)上节点的值都比它小

  3.所以这个node在父节点的左子树上,所以我们要找的是它的父节点。

    1)如果有parent节点,就一直往上找到第一个比它大的节点

    2)如果没有parent节点,那就从根往下找,记录path上的上一个节点,如果当前节点上的值比目标节点大,那就往左拐,否则往右拐,如果相同就返回path的上一个节点

 

 

pocketGems的面经

 1     public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
 2         if(root == null) {
 3             return null;
 4         }
 5         Stack<TreeNode> stack = new Stack<TreeNode>();
 6         TreeNode cur = root;
 7         TreeNode pre = null;
 8         while(cur != null || !stack.isEmpty()) {
 9             while(cur != null) {
10                 stack.push(cur);
11                 cur = cur.left;
12             }
13             cur = stack.pop();
14             if(pre != null && pre == p) {
15                 return cur;
16             }
17             pre = cur;
18             cur = cur.right;
19         }
20         return null;
21     }

 更新

findNodeWithParent

 1 public class SuccessorInBST {
 2     public static TreeNodewParent successor(TreeNodewParent node) {
 3         if(node.right != null) {
 4             node = node.right;
 5             while(node.left != null) {
 6                 node = node.left;
 7             }
 8             return node;
 9         }
10         TreeNodewParent parent = node.parent;
11         while(parent.val < node.val) {
12             node = parent;
13             parent = parent.parent;
14         }
15         return parent;
16     }

 

posted @ 2016-08-22 04:38  warmland  阅读(232)  评论(0编辑  收藏  举报