623. Add One Row to Tree #164
-
Topics: Given the Note that the The adding rule is:
Example 1:
Example 2:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We can use either Breadth-First Search (BFS) or Depth-First Search (DFS) to traverse the tree. The idea is to add the new row at the specified depth. Approach:
Steps:
Let's implement this solution in PHP: 623. Add One Row to Tree <?php
// Definition for a binary tree node.
class TreeNode {
public $val = null;
public $left = null;
public $right = null;
function __construct($val = 0, $left = null, $right = null) {
$this->val = $val;
$this->left = $left;
$this->right = $right;
}
}
function addOneRow($root, $val, $depth) {
if ($depth == 1) {
// Create a new root with value $val, and set the original tree as its left child
return new TreeNode($val, $root, null);
}
// Perform BFS to find nodes at depth - 1
$queue = [$root];
$current_depth = 1;
// Traverse until we reach depth - 1
while (!empty($queue) && $current_depth < $depth - 1) {
$size = count($queue);
for ($i = 0; $i < $size; $i++) {
$node = array_shift($queue);
if ($node->left !== null) {
$queue[] = $node->left;
}
if ($node->right !== null) {
$queue[] = $node->right;
}
}
$current_depth++;
}
// At this point, we are at depth - 1. Add the new row.
foreach ($queue as $node) {
// Save the original left and right children
$old_left = $node->left;
$old_right = $node->right;
// Insert new nodes with value $val
$node->left = new TreeNode($val, $old_left, null);
$node->right = new TreeNode($val, null, $old_right);
}
return $root;
}
// Helper function to print the tree in level order for visualization
function printTree($root) {
$result = [];
$queue = [$root];
while (!empty($queue)) {
$node = array_shift($queue);
if ($node !== null) {
$result[] = $node->val;
$queue[] = $node->left;
$queue[] = $node->right;
} else {
$result[] = null;
}
}
// Remove trailing null values
while (end($result) === null) {
array_pop($result);
}
return $result;
}
// Test case 1
$root = new TreeNode(4,
new TreeNode(2, new TreeNode(3), new TreeNode(1)),
new TreeNode(6, new TreeNode(5), null));
$val = 1;
$depth = 2;
$new_tree = addOneRow($root, $val, $depth);
print_r(printTree($new_tree)); // Output should be [4, 1, 1, 2, null, null, 6, 3, 1, 5]
// Test case 2
$root = new TreeNode(4,
new TreeNode(2, new TreeNode(3), new TreeNode(1)),
null);
$val = 1;
$depth = 3;
$new_tree = addOneRow($root, $val, $depth);
print_r(printTree($new_tree)); // Output should be [4, 2, null, 1, 1, 3, null, null, 1]
?> Explanation:
Time Complexity:
Test Cases:
This solution ensures that the tree is modified correctly according to the problem's rules. |
Beta Was this translation helpful? Give feedback.
We can use either Breadth-First Search (BFS) or Depth-First Search (DFS) to traverse the tree. The idea is to add the new row at the specified depth.
Approach:
depth
is 1, we need to add the new node as the new root of the tree.depth - 1
, then:val
as left and right children of each node.Steps:
depth == 1
, create a new root and attach the original tree as the left child of the new root.