[leetcode]623. Add One Row to Tree

Posted stAr_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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);
    }

 

以上是关于[leetcode]623. Add One Row to Tree的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 623. Add One Row to Tree

[leetcode]623. Add One Row to Tree

623. Add One Row to Tree

623. Add One Row to Tree

623. Add One Row to Tree 将一行添加到树中

623. 在二叉树中增加一行