package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

/**
 * 623. Add One Row to Tree
 *
 * 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.
 */
public class _623 {

    public TreeNode addOneRow(TreeNode root, int v, int d) {
        if (d == 1) {
            TreeNode newRoot = new TreeNode(v);
            newRoot.left = root;
            return newRoot;
        } else {
            dfs(root, v, d);
            return root;
        }
    }

    private void dfs(TreeNode root, int v, int d) {
        if (root == null) {
            return;
        }
        if (d == 2) {
            TreeNode newLeft = new TreeNode(v);
            TreeNode newRight = new TreeNode(v);
            newLeft.left = root.left;
            newRight.right = root.right;
            root.left = newLeft;
            root.right = newRight;
        } else {
            dfs(root.left, v, d - 1);
            dfs(root.right, v, d - 1);
        }
    }

}

@@ -1365,16 +1365,57 @@
 _623 %7B%0A%0A
+    public static class Solution1 %7B%0A%0A    
     publ
@@ -1467,32 +1467,36 @@
 nt d) %7B%0A        
+    
 if (d == 1) %7B%0A  
@@ -1497,32 +1497,36 @@
 ) %7B%0A            
+  
+  
 TreeNode newRoot
@@ -1553,24 +1553,28 @@
             
+    
 newRoot.left
@@ -1590,24 +1590,28 @@
             
+   
+ 
 return newRo
@@ -1614,32 +1614,36 @@
 ewRoot;%0A        
+    
 %7D else %7B%0A       
@@ -1627,32 +1627,36 @@
        %7D else %7B%0A
+    
             dfs(
@@ -1676,24 +1676,28 @@
             
+    
 return root;
@@ -1705,25 +1705,37 @@
         
+    
 %7D%0A    
+ 
-%7D%0A%0A
+   %7D%0A%0A    
     priv
@@ -1782,24 +1782,28 @@
 ) %7B%0A        
+   
+ 
 if (root == 
@@ -1826,16 +1826,20 @@
     
+    
 return;%0A
     
@@ -1834,16 +1834,20 @@
 return;%0A
+    
         
@@ -1848,32 +1848,36 @@
       %7D%0A        
+    
 if (d == 2) %7B%0A  
@@ -1870,24 +1870,28 @@
  (d == 2) %7B%0A
+    
             
@@ -1930,32 +1930,36 @@
 v);%0A            
+ 
+   
 TreeNode newRigh
@@ -1991,16 +1991,20 @@
         
+    
 newLeft.
@@ -2029,24 +2029,28 @@
             
+  
+  
 newRight.rig
@@ -2074,24 +2074,28 @@
             
+    
 root.left = 
@@ -2111,24 +2111,28 @@
             
+   
+ 
 root.right =
@@ -2142,32 +2142,36 @@
 wRight;%0A        
+    
 %7D else %7B%0A       
@@ -2155,32 +2155,36 @@
        %7D else %7B%0A
+    
             dfs(
@@ -2209,32 +2209,36 @@
 1);%0A            
+    
 dfs(root.right, 
@@ -2240,32 +2240,46 @@
 ght, v, d - 1);%0A
+            %7D%0A
         %7D%0A    %7D%0A

refactor 623