[leetcode]623. Add One Row to Tree

public TreeNode addOneRow(TreeNode root, int v, int d) {
        if (d==1)
        {
            TreeNode res = new TreeNode(v);
            res.left = root;
            return res;
        }
        helper(root,v,d-1,1);
        return root;
    }
    public void helper(TreeNode root, int v, int d,int cur)
    {
        if (root==null) return;
        if (cur==d)
        {
            TreeNode l = root.left;
            TreeNode r = root.right;
            root.left = new TreeNode(v);
            root.right = new TreeNode(v);
            root.left.left = l;
            root.right.right = r;
            return;
        }
        helper(root.left,v,d,cur+1);
        helper(root.right,v,d,cur+1);
    }

 

posted @ 2018-02-03 13:09  stAr_1  阅读(117)  评论(0编辑  收藏  举报