https://leetcode.com/problems/add-one-row-to-tree
X. BFS - order traversal
Space complexity : . The size of the or queue can grow upto only. Here, refers to the number of maximum number of nodes at any level in the given tree.
In addition to use
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.
X. BFS - order traversal
Space complexity : . The size of the or queue can grow upto only. Here, refers to the number of maximum number of nodes at any level in the given tree.
public TreeNode addOneRow(TreeNode t, int v, int d) { if (d == 1) { TreeNode n = new TreeNode(v); n.left = t; return n; } Queue < TreeNode > queue = new LinkedList < > (); queue.add(t); int depth = 1; while (depth < d - 1) { Queue < TreeNode > temp = new LinkedList < > (); while (!queue.isEmpty()) { TreeNode node = queue.remove(); if (node.left != null) temp.add(node.left); if (node.right != null) temp.add(node.right); } queue = temp; depth++; } while (!queue.isEmpty()) { TreeNode node = queue.remove(); TreeNode temp = node.left; node.left = new TreeNode(v); node.left.left = temp; temp = node.right; node.right = new TreeNode(v); node.right.right = temp; } return t; }
public TreeNode addOneRow(TreeNode t, int v, int d) { if (d == 1) { TreeNode n = new TreeNode(v); n.left = t; return n; } insert(v, t, 1, d); return t; } public void insert(int val, TreeNode node, int depth, int n) { if (node == null) return; if (depth == n - 1) { TreeNode t = node.left; node.left = new TreeNode(val); node.left.left = t; t = node.right; node.right = new TreeNode(val); node.right.right = t; } else { insert(val, node.left, depth + 1, n); insert(val, node.right, depth + 1, n); } }https://discuss.leetcode.com/topic/92876/c-java-10-line-solution-no-helper
In addition to use
1
to indicate attach to left node
as required, we can also use 0
to indicate attach to right node
; public TreeNode addOneRow(TreeNode root, int v, int d) {
if (d == 0 || d == 1) {
TreeNode newroot = new TreeNode(v);
newroot.left = d == 1 ? root : null;
newroot.right = d == 0 ? root : null;
return newroot;
}
if (root != null && d >= 2) {
root.left = addOneRow(root.left, v, d > 2 ? d - 1 : 1);
root.right = addOneRow(root.right, v, d > 2 ? d - 1 : 0);
}
return root;
}
https://discuss.leetcode.com/topic/92964/java-three-methods-one-bfs-and-two-dfs/- DFS without helper function, similar to @alexander 's top post
public TreeNode addOneRow(TreeNode root, int v, int d) {
if (d < 2) {
TreeNode newroot = new TreeNode(v);
if (d == 0) newroot.right = root;
else newroot.left = root;
return newroot;
}
if (root == null) return null;
root.left = addOneRow(root.left, v, d == 2 ? 1 : d-1);
root.right = addOneRow(root.right, v, d == 2 ? 0 : d-1);
return root;
}
https://leetcode.com/articles/add-one-row-in-a-tree/class Node{ Node(TreeNode n,int d){ node=n; depth=d; } TreeNode node; int depth; } public TreeNode addOneRow(TreeNode t, int v, int d) { if (d == 1) { TreeNode n = new TreeNode(v); n.left = t; return n; } Stack<Node> stack=new Stack<>(); stack.push(new Node(t,1)); while(!stack.isEmpty()) { Node n=stack.pop(); if(n.node==null) continue; if (n.depth == d - 1 ) { TreeNode temp = n.node.left; n.node.left = new TreeNode(v); n.node.left.left = temp; temp = n.node.right; n.node.right = new TreeNode(v); n.node.right.right = temp; } else{ stack.push(new Node(n.node.left, n.depth + 1)); stack.push(new Node(n.node.right, n.depth + 1)); } } return t; }