285. Inorder Successor in BST
2017-08-09 00:00
134 查看
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
Note: If the given node has no in-order successor in the tree, return
null.
/** * 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 null; } if(p.right != null){ //右边不为空直接返回右边的最左个节点 p = p.right; while(p.left != null){ p = p.left; } return p; }else{ TreeNode parent = null; while(root != p){ if(root.val > p.val){ parent = root;//只有向左边走的时候才记录 root = root.left; }else{ root = root.right; } } return parent; } } }
相关文章推荐
- 285. Inorder Successor in BST
- 285. Inorder Successor in BST
- 285. Inorder Successor in BST
- 1st round, 285 Inorder Successor in BST
- 285. Inorder Successor in BST
- 285. Inorder Successor in BST
- [leetcode 285] Inorder Successor in BST---查找二叉搜索树中某个节点在中序遍历中的后续节点
- Medium 285题 Inorder Successor in BST
- [LeetCode285]Inorder Successor in BST
- 285. Inorder Successor in BST
- Tree_Graph Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点 @CareerCup
- LeetCode-Inorder Successor in BST
- [LeetCode] Inorder Successor in BST
- Inorder Successor in BST
- 285. Inorder Successor in BST
- Leetcode: Inorder Successor in BST
- 如何用C++递归来查找BST中的根节点的inorder successor
- [Lintcode] Inorder Successor in BST
- [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点
- *Inorder Successor in BST