美文网首页
285. Inorder Successor in BST

285. Inorder Successor in BST

作者: 夜皇雪 | 来源:发表于2016-12-13 10:37 被阅读0次
/**
 * 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(root.val<=p.val){
            return inorderSuccessor(root.right,p);
        }else{
            TreeNode left=inorderSuccessor(root.left,p);
            return left==null? root:left;
        }
    }
}

相关文章

网友评论

      本文标题:285. Inorder Successor in BST

      本文链接:https://www.haomeiwen.com/subject/ouwvmttx.html