Golang solution


  • 0
    S
    func addOneRowR(root *TreeNode, v, d, cd int) {
        if (root == nil) {
            return;
        }
            
        if (cd == d) {
            savLeft := root.Left
            root.Left = &TreeNode{Val : v}
            root.Left.Left = savLeft
                
            savRight := root.Right
            root.Right = &TreeNode{Val : v}
            root.Right.Right = savRight
            return
        }
        addOneRowR(root.Left, v, d, cd + 1)
        addOneRowR(root.Right, v, d, cd + 1)
    }
        
    func addOneRow(root *TreeNode, v int, d int) *TreeNode {
        if (d == 1) {
            newRoot := &TreeNode{Val : v, Left : root }
            return newRoot;
        }
        addOneRowR(root, v, d, 2);
        return root;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.