Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/623.html
623. Add One Row to Tree
Level
Medium
Description
Given the root of a binary tree, then value v
and depth d
, you need to add a row of nodes with value v
at the given depth d
. The root node is at depth 1.
The adding rule is: given a positive integer depth d
, for each NOT null tree nodes N
in depth d-1
, create two tree nodes with value v
as N's
left subtree root and right subtree root. And N's
original left subtree should be the left subtree of the new left subtree root, its original right subtree should be the right subtree of the new right subtree root. If depth d
is 1 that means there is no depth d-1 at all, then create a tree node with value v as the new root of the whole original tree, and the original tree is the new root’s left subtree.
Example 1:
Input:
A binary tree as following:
4
/ \
2 6
/ \ /
3 1 5
v = 1
d = 2
Output:
4
/ \
1 1
/ \
2 6
/ \ /
3 1 5
Example 2:
Input:
A binary tree as following:
4
/
2
/ \
3 1
v = 1
d = 3
Output:
4
/
2
/ \
1 1
/ \
3 1
Note:
- The given d is in range [1, maximum depth of the given tree + 1].
- The given binary tree has at least one tree node.
Solution
If d
is 1, then simply create a new root with value v
, let the root
be the left child of the new root, and return the new root.
If d
is greater than 1, then do breadth first search to find all the nodes at depth d - 1
. For each node at depth d - 1
, add two children with value v
, and set the two children’s left child and right child to be the node’s original left child and right child, respectively. Finally, return the root.
-
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode addOneRow(TreeNode root, int v, int d) { if (root == null) return root; if (d == 1) { TreeNode newRoot = new TreeNode(v); newRoot.left = root; return newRoot; } Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.offer(root); int level = 2; while (!queue.isEmpty() && level < d) { int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); TreeNode left = node.left, right = node.right; if (left != null) queue.offer(left); if (right != null) queue.offer(right); } level++; } while (!queue.isEmpty()) { TreeNode node = queue.poll(); TreeNode left = node.left, right = node.right; node.left = new TreeNode(v); node.left.left = left; node.right = new TreeNode(v); node.right.right = right; } return root; } }
-
// OJ: https://leetcode.com/problems/add-one-row-to-tree/ // Time: O(N) // Space: O(H) class Solution { public: TreeNode* addOneRow(TreeNode* root, int v, int d, bool left = true) { if (d <= 0) return root; if (d == 1) { auto r = new TreeNode(v); if (left) r->left = root; else r->right = root; return r; } if (root) { root->left = addOneRow(root->left, v, d - 1, true); root->right = addOneRow(root->right, v, d - 1, false); } return root; } };
-
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def addOneRow( self, root: Optional[TreeNode], val: int, depth: int ) -> Optional[TreeNode]: def dfs(root, d): if root is None: return if d == depth - 1: root.left = TreeNode(val, root.left, None) root.right = TreeNode(val, None, root.right) return dfs(root.left, d + 1) dfs(root.right, d + 1) if depth == 1: return TreeNode(val, root) dfs(root, 1) return root ############ # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def addOneRow(self, root, v, d): """ :type root: TreeNode :type v: int :type d: int :rtype: TreeNode """ # BFS is even better because it terminates much earlier dummy = TreeNode(-1) dummy.left = root stack = [(1, dummy, 0)] while stack: p, node, h = stack.pop() if not node: continue if p == 1: stack.extend([(1, node.right, h + 1), (1, node.left, h + 1), (0, node, h + 1)]) elif h == d: left = node.left right = node.right node.left, node.right = map(TreeNode, (v, v)) node.left.left = left node.right.right = right return dummy.left
-
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func addOneRow(root *TreeNode, val int, depth int) *TreeNode { if depth == 1 { return &TreeNode{Val: val, Left: root} } var dfs func(root *TreeNode, d int) dfs = func(root *TreeNode, d int) { if root == nil { return } if d == depth-1 { l, r := &TreeNode{Val: val, Left: root.Left}, &TreeNode{Val: val, Right: root.Right} root.Left, root.Right = l, r return } dfs(root.Left, d+1) dfs(root.Right, d+1) } dfs(root, 1) return root }
-
/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ function addOneRow( root: TreeNode | null, val: number, depth: number, ): TreeNode | null { if (depth === 1) { return new TreeNode(val, root); } const queue = [root]; for (let i = 1; i < depth - 1; i++) { const n = queue.length; for (let j = 0; j < n; j++) { const { left, right } = queue.shift(); left && queue.push(left); right && queue.push(right); } } for (const node of queue) { node.left = new TreeNode(val, node.left); node.right = new TreeNode(val, null, node.right); } return root; }