首页 > 其他 > 详细

Inorder Successor in BST

时间:2015-11-26 14:51:11      阅读:134      评论:0      收藏:0      [点我收藏+]

Boring

public class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        return helper(root, p, null);
    }
    public TreeNode helper(TreeNode root, TreeNode p, TreeNode pre) {
        if (root == p) {
            if (root.right != null) {
                TreeNode tmp = root.right;
                while (tmp.left != null) {
                    tmp = tmp.left;
                }
                return tmp;
            } else {
                return pre;
            }
        }
        if (root.val < p.val && root.right != null) {
            return helper(root.right, p, pre);
        } else if (root.val > p.val && root.left != null) {
            return helper(root.left, p, root);
        }
        return null;
    }
}

 

Inorder Successor in BST

原文:http://www.cnblogs.com/vision-love-programming/p/4997450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!