No helper and Recursion solution.

  • 0
    def addOneRow(self, root, v, d):
        :type root: TreeNode
        :type v: int
        :type d: int
        :rtype: TreeNode
        if d>2:
            if root.left:
                self.addOneRow(root.left, v, d-1)
            if root.right:
                self.addOneRow(root.right, v, d-1)
        if d == 1:
            new_root = TreeNode(v)
            new_root.left = root
            return new_root
        if d == 2:
            node2 = TreeNode(v)
            node1 = TreeNode(v)
            l = root.left
            r = root.right
            root.left = node2
            root.right = node1
            node2.left = l
            node1.right = r
        return root

Log in to reply

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