File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public TreeNode mergeTrees (TreeNode root1 , TreeNode root2 ) {
3
+
4
+ if (root1 == null && root2 == null ) return null ;
5
+
6
+ int val1 = root1 != null ? root1 .val : 0 ;
7
+ int val2 = root2 != null ? root2 .val : 0 ;
8
+
9
+ TreeNode root = new TreeNode (val1 +val2 );
10
+
11
+ // merge left side of trees if they are not null
12
+ root .left = mergeTrees ((root1 != null && root1 .left != null ) ? root1 .left : null , (root2 != null && root2 .left != null ) ? root2 .left : null );
13
+
14
+ // merge righ side of trees if they are not null
15
+ root .right = mergeTrees ((root1 != null && root1 .right != null ) ? root1 .right : null , (root2 !=null && root2 .right != null ) ? root2 .right : null );
16
+
17
+ return root ;
18
+ }
19
+ }
20
+
21
+
You can’t perform that action at this time.
0 commit comments