@@ -0,0 +1,2167 @@
+/**%0A * Definition for a binary tree node.%0A * public class TreeNode %7B%0A *     int val;%0A *     TreeNode left;%0A *     TreeNode right;%0A *     TreeNode(int x) %7B val = x; %7D%0A * %7D%0A * 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. %0A * The root node is at depth 1.%0A * The adding rule is: %0A *   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. %0A *   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. %0A *   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.%0A */%0Apublic class Solution %7B%0A  public TreeNode addOneRow(TreeNode root, int v, int d) %7B%0A    Queue%3CTreeNode%3E queue = new LinkedList%3CTreeNode%3E();%0A    queue.add(root);%0A%0A    // Edge case where we are adding a new root node.%0A    if (d == 1) %7B%0A      TreeNode temp = new TreeNode(v);%0A      temp.left = root;%0A      return temp;%0A    %7D%0A%0A    int current = 1;%0A    int next = 0;%0A    int depth = 1;   // depth counter as we iterate%0A%0A    while (!queue.isEmpty()) %7B%0A      TreeNode node = queue.poll();%0A      current --;%0A%0A      if (d - 1 == depth) %7B%0A        // Store the left and right pointers to reattach.%0A        TreeNode left = node.left;%0A        TreeNode right = node.right;%0A%0A        // Create the new row.%0A        node.left = new TreeNode(v);%0A        node.right = new TreeNode(v);%0A%0A        // Reattach the old pointers to the new row.%0A        node.right.right = right;%0A        node.left.left = left;%0A      %7D else %7B%0A        if (node.left != null) %7B%0A          queue.add(node.left);%0A          next ++;%0A        %7D%0A%0A        if (node.right != null) %7B%0A          queue.add(node.right);%0A          next ++;%0A        %7D%0A      %7D%0A%0A      if (current == 0) %7B%0A        if (depth == d) return root;%0A        depth ++;%0A        current = next;%0A        next = 0;%0A      %7D%0A    %7D%0A%0A    return root;%0A  %7D%0A%7D%0A

Add one row to tree 623