题解 | #恢复二叉搜索树#

恢复二叉搜索树

http://www.nowcoder.com/practice/67c7172122b54b748e78eac7b183b5f3

class Solution {
    private TreeNode t1,t2,pre;
    public void recoverTree(TreeNode root) {
        inOrder(root);
        int temp = t1.val;
        t1.val = t2.val;
        t2.val = temp;
    }

    public void inOrder(TreeNode root){
        if(root == null)
            return;

        inOrder(root.left);

        if(pre!=null && pre.val > root.val){
            if(t1==null)
                t1 = pre;
            t2 = root;
        }
        pre = root;
        inOrder(root.right);
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务