Inorder Successor in Binary Search Tree
2016-12-13 14:45
344 查看
Given a binary search tree (See Definition) and a node in it, find
the in-order successor of that node in the BST.
If the given node has no in-order successor in the tree, return
Notice
It's guaranteed p is one node in the given tree. (You can directly compare the memory address to find p)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if(root == null) {
return root;
}
TreeNode successor = null;
while(root != null && root.val != p.val) {
if(root.val > p.val) {
successor = root;
root = root.left;
} else {
root = root.right;
}
}
//p is found
//p doesn't have right subtree
if(root.right == null) {
return successor;
}
//get the most left child of the right subtree
root = root.right;
while(root.left != null) {
root = root.left;
}
return root;
}
}
Notice that this is BST. So the successor node will be at two possible position:
its last parent node that is larger then itself;
the leftmost child of its right subtree
the in-order successor of that node in the BST.
If the given node has no in-order successor in the tree, return
null.
Notice
It's guaranteed p is one node in the given tree. (You can directly compare the memory address to find p)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if(root == null) {
return root;
}
TreeNode successor = null;
while(root != null && root.val != p.val) {
if(root.val > p.val) {
successor = root;
root = root.left;
} else {
root = root.right;
}
}
//p is found
//p doesn't have right subtree
if(root.right == null) {
return successor;
}
//get the most left child of the right subtree
root = root.right;
while(root.left != null) {
root = root.left;
}
return root;
}
}
Notice that this is BST. So the successor node will be at two possible position:
its last parent node that is larger then itself;
the leftmost child of its right subtree
相关文章推荐
- LintCode : Inorder Successor in Binary Search Tree
- [Google] Inorder Successor in Binary Search Tree, Solution
- [Google] Inorder Successor in Binary Search Tree, Solution
- Inorder Successor in Binary Search Tree
- Tree_Graph Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点 @CareerCup
- Inorder Successor in Binary Search Tree
- Inorder Successor in Binary Search Tree
- Inorder Successor in Binary Search Tree
- Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点
- Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree
- [LeetCode255]Verify Preorder Sequence in Binary Search Tree
- Verify Preorder Sequence in Binary Search Tree
- LeetCode "Verify Preorder Sequence in Binary Search Tree"
- BT search 2 level order, Construct Binary Tree from Preorder and Inorder Traversal
- 255. Verify Preorder Sequence in Binary Search Tree
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
- 中序遍历二叉排序树:BinarySearchTree:Create a tree and InorderTree
- Leetcode: Verify Preorder Sequence in Binary Search Tree
- Binary Tree Inorder Traversal, Binary Search Tree Iterator
- Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree