LeetCode Solutions
623. Add One Row to Tree
Time: $O(n)$ Space: $O(n)$
class Solution {
public:
TreeNode* addOneRow(TreeNode* root, int v, int d) {
if (d == 1) {
TreeNode* newRoot = new TreeNode(v);
newRoot->left = root;
return newRoot;
}
int depth = 0;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
++depth;
for (int sz = q.size(); sz > 0; --sz) {
TreeNode* node = q.front();
q.pop();
if (node->left)
q.push(node->left);
if (node->right)
q.push(node->right);
if (depth == d - 1) {
TreeNode* cachedLeft = node->left;
TreeNode* cachedRight = node->right;
node->left = new TreeNode(v);
node->right = new TreeNode(v);
node->left->left = cachedLeft;
node->right->right = cachedRight;
}
}
if (depth == d - 1)
break;
}
return root;
}
};
class Solution {
public TreeNode addOneRow(TreeNode root, int v, int d) {
if (d == 1) {
TreeNode newRoot = new TreeNode(v);
newRoot.left = root;
return newRoot;
}
int depth = 0;
Queue<TreeNode> q = new ArrayDeque<>(Arrays.asList(root));
while (!q.isEmpty()) {
++depth;
for (int sz = q.size(); sz > 0; --sz) {
TreeNode node = q.poll();
if (node.left != null)
q.offer(node.left);
if (node.right != null)
q.offer(node.right);
if (depth == d - 1) {
TreeNode cachedLeft = node.left;
TreeNode cachedRight = node.right;
node.left = new TreeNode(v);
node.right = new TreeNode(v);
node.left.left = cachedLeft;
node.right.right = cachedRight;
}
}
if (depth == d - 1)
break;
}
return root;
}
}