Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.
Note that the root node is at depth 1.
The adding rule is:
depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.cur's original left subtree should be the left subtree of the new left subtree root.cur's original right subtree should be the right subtree of the new right subtree root.depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.Example 1:
Input: root = [4,2,6,3,1,5], val = 1, depth = 2 Output: [4,1,1,2,null,null,6,3,1,5]
Example 2:
Input: root = [4,2,null,3,1], val = 1, depth = 3 Output: [4,2,null,1,1,3,null,null,1]
Constraints:
[1, 104].[1, 104].-100 <= Node.val <= 100-105 <= val <= 1051 <= depth <= the depth of tree + 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to adding a row to a tree means we explore every possible place to insert this new row. We essentially check every node in the tree to see if it's where we want to add the new row.
Here's how the algorithm would work step-by-step:
def add_one_row(root, value, depth): if depth == 1:
new_node = TreeNode(value)
new_node.left = root
return new_node
def add_row_recursive(node, current_depth):
if not node:
return
if current_depth == depth - 1:
# Insert new nodes with the given value
temp_left = node.left
node.left = TreeNode(value)
node.left.left = temp_left
temp_right = node.right
node.right = TreeNode(value)
node.right.right = temp_right
else:
# Recursively call the method on the left and right subtrees
add_row_recursive(node.left, current_depth + 1)
# Now traverse the right sub-tree
add_row_recursive(node.right, current_depth + 1)
add_row_recursive(root, 1)
return rootThe key idea is to traverse the tree level by level until we reach the desired depth. At that depth, we insert the new row of nodes with the given value, effectively pushing the original nodes down a level.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def add_one_row(root, value, depth):
if depth == 1:
new_node = TreeNode(value)
new_node.left = root
return new_node
def traverse_tree(node, current_depth):
if not node:
return
if current_depth == depth - 1:
# We've reached the level above insertion.
new_left_node = TreeNode(value)
new_right_node = TreeNode(value)
new_left_node.left = node.left
new_right_node.right = node.right
node.left = new_left_node
node.right = new_right_node
else:
# Continue traversal to reach target depth.
traverse_tree(node.left, current_depth + 1)
traverse_tree(node.right, current_depth + 1)
# Initiate the recursive traversal.
traverse_tree(root, 1)
return root| Case | How to Handle |
|---|---|
| Null root | If root is null and depth is 1, return a new tree node with value v as the root, with two children having null values, otherwise return null. |
| Depth is less than 1 | Handle depths less than 1 by either treating it as an error or returning the original root. |
| Integer overflow when calculating tree size or intermediate values in recursive calls. | Ensure the values used for v, depth, and any calculations within the tree structure stay within integer limits. |
| Adding row at depth 1 with non-empty tree | Create a new root node with value v and make original tree root the left child of this new root and right child set to null if the original root does not have a right child, otherwise the right child should be set to null. |
| Large tree depth potentially leading to stack overflow during recursion. | Consider using an iterative approach (e.g., level order traversal) instead of recursion for very deep trees to avoid stack overflow errors. |
| Skewed tree (all nodes on one side) | The algorithm should handle skewed trees correctly, inserting new nodes at the specified depth regardless of the tree's balance. |
| Depth greater than height of the tree. | If depth is greater than the height of the tree, the new row will simply not be added, and the original tree will be returned as there is nothing to process at that depth. |
| Value 'v' is a boundary value close to the maximum or minimum integer possible. | The value being inserted for a node should be validated to ensure insertion does not cause integer overflow issues in subsequent operations. |