623. Add One Row to Tree

Jesus PF
2 min readDec 10, 2020

--

LeetCode level medium question involving BFS and binary trees

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 = 1d = 2Output:
4
/ \
1 1
/ \
2 6
/ \ /
3 1 5

Example 2:

Input: 
A binary tree as following:
4
/
2
/ \
3 1
v = 1d = 3Output:
4
/
2
/ \
1 1
/ \
3 1

Note:

  1. The given d is in range [1, maximum depth of the given tree + 1].
  2. The given binary tree has at least one tree node.

Result:

SuccessDetailsRuntime: 0 ms, faster than 100.00% of Java online submissions for Add One Row to Tree.Memory Usage: 38.6 MB, less than 87.71% of Java online submissions for Add One Row to Tree.

Solution:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode addOneRow(TreeNode root, int v, int d) {
//iterate three BFS
//Queue
// consume N
//count three level
//if three level matches, call another f to modify
//end
TreeNode result=root;
Queue<TreeNode> queue= new LinkedList<TreeNode>();
if(root != null )queue.add(root);
if(d ==0 ) return null;
if(d == 1){
TreeNode newRoot = new TreeNode(v);
newRoot.left = root;
return newRoot;
}
int queueLevel=1;
while( !queue.isEmpty() ){
int itemsInQueue= queue.size();
if(++queueLevel == d){
//add row
addRow(queue,v);
}else{
while(itemsInQueue-- > 0){
TreeNode current = queue.poll();
if(current.left != null ){
queue.add(current.left);
}
if(current.right != null ){
queue.add(current.right);
}
}
}
}
return result;
}

public void addRow(Queue<TreeNode> queue,int v){
int itemsInQueue= queue.size();
while(itemsInQueue > 0){
TreeNode current = queue.poll();
//replaces item from left,
TreeNode nodeLeft = new TreeNode(v);
if(current.left != null){
nodeLeft.left =current.left;
}
current.left = nodeLeft;
//then from rigth
TreeNode nodeRight = new TreeNode(v);
if(current.right != null){
nodeRight.right =current.right;
}
current.right = nodeRight;
//inc counter
itemsInQueue--;
}
}

}

--

--

Jesus PF
Jesus PF

Written by Jesus PF

I am en electronics engineer, graduated from ITESM. Experienced working as functional validation and software development.

No responses yet