package leetcode;

/**
 * https://leetcode.com/problems/merge-two-binary-trees/
 */
public class Problem617 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }

        @Override
        public String toString() {
            return "" + val;
        }
    }

    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem617 prob = new Problem617();

        TreeNode t1 = new TreeNode(1);
        t1.left = new TreeNode(3);
        t1.right = new TreeNode(2);
        t1.left.left = new TreeNode(5);

        TreeNode t2 = new TreeNode(2);
        t2.left = new TreeNode(1);
        t2.left.right = new TreeNode(4);
        t2.right = new TreeNode(3);
        t2.right.right = new TreeNode(7);

        prob.mergeTrees(t1, t2);
    }
}

@@ -431,16 +431,30 @@
     
-// TODO%0A
+if (t1 == null) %7B%0A    
     
@@ -468,119 +468,101 @@
 urn 
-null;%0A
+t2;%0A   
     
+ 
 %7D
-%0A
 %0A    
-public static void main(String%5B%5D args) %7B%0A        Problem617 prob = new Problem617();%0A
+    merge(t1, t2);%0A        return t1;
 %0A    
+%7D%0A%0A
     
+private static void merge(
 Tree
@@ -568,22 +568,17 @@
 eNode t1
- = new
+,
  TreeNod
@@ -570,36 +570,38 @@
 ode t1, TreeNode
-(1);
+ t2) %7B
 %0A        t1.left
@@ -597,151 +597,190 @@
     
-t1.left
+if (t2
  =
+=
  n
-ew TreeNode(3);%0A        t1.right = new TreeNode(2);%0A        t1.left.left = new TreeNode(5);%0A%0A        TreeNode t2 = new TreeNode(2);%0A
+ull) %7B%0A            return;%0A        %7D%0A        if (t1 != null && t2 != null) %7B%0A            t1.val += t2.val;%0A        %7D%0A        if (t1.left == null && t2.left != null) %7B%0A    
     
@@ -788,17 +788,17 @@
    t
-2
+1
 .left = 
 new 
@@ -797,65 +797,80 @@
 t = 
-new TreeNode(1);%0A        t2.left.right = new TreeNode(4);
+t2.left;%0A        %7D else %7B%0A            merge(t1.left, t2.left);%0A        %7D
 %0A   
@@ -878,18 +878,22 @@
     
-t2
+if (t1
 .right =
  new
@@ -892,34 +892,49 @@
 ht =
+=
  n
-ew TreeNode(3);%0A
+ull && t2.right != null) %7B%0A   
         
 t2.r
@@ -933,75 +933,95 @@
     
-t2
+ t1
 .right
+ = t2
 .right
- = new TreeNode(7);%0A%0A        prob.mergeTrees(t1, t2);
+;%0A        %7D else %7B%0A            merge(t1.right, t2.right);%0A        %7D
 %0A   

Solve problem 617