码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]623. Add One Row to Tree

时间:2018-02-03 15:57:03      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:pos   pre   code   tree   nod   ret   row   ==   color   

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);
    }

 

[leetcode]623. Add One Row to Tree

标签:pos   pre   code   tree   nod   ret   row   ==   color   

原文地址:https://www.cnblogs.com/stAr-1/p/8409218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!