|
| 1 | +class Solution { |
| 2 | + public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { |
| 3 | + //If t1 == NULL -> t2 |
| 4 | + //If t2 == NULL -> t2 |
| 5 | + // Sum (t1,t2) |
| 6 | + |
| 7 | + //Preorder Traversal (P -> L -> R) |
| 8 | + if(t1 == null) return t2; |
| 9 | + if(t2 == null) return t1; |
| 10 | + t1.val+=t2.val; |
| 11 | + t1.left = mergeTrees(t1.left, t2.left); |
| 12 | + t1.right = mergeTrees(t1.right, t2.right); |
| 13 | + return t1; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +class Solution { |
| 20 | + public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { |
| 21 | + //If t1 == NULL -> t2 |
| 22 | + //If t2 == NULL -> t2 |
| 23 | + // Sum (t1,t2) |
| 24 | + |
| 25 | + if(t1 == null) return t2; |
| 26 | + if(t2 == null) return t1; |
| 27 | + |
| 28 | + Stack<TreeNode[]> st = new Stack<>(); |
| 29 | + st.push(new TreeNode[]{t1,t2}); |
| 30 | + |
| 31 | + //While is not empty process the nodes |
| 32 | + while(!st.isEmpty()) { |
| 33 | + TreeNode[] curr = st.pop(); |
| 34 | + |
| 35 | + //Process the node |
| 36 | + curr[0].val+=curr[1].val; |
| 37 | + |
| 38 | + //1. curr[0] = null, curr[1] != null -> curr[1] |
| 39 | + //2. curr[1] = null, curr[0] != null -> curr[0] |
| 40 | + //3. both not null, add it stack |
| 41 | + |
| 42 | + //Left Tree |
| 43 | + if(curr[0].left == null) { |
| 44 | + curr[0].left = curr[1].left; |
| 45 | + } |
| 46 | + else if(curr[1].left != null) { |
| 47 | + st.push(new TreeNode[]{curr[0].left, curr[1].left}); |
| 48 | + } |
| 49 | + |
| 50 | + //Right Tree |
| 51 | + if(curr[0].right == null) { |
| 52 | + curr[0].right = curr[1].right; |
| 53 | + } |
| 54 | + else if(curr[1].right != null) { |
| 55 | + st.push(new TreeNode[]{curr[0].right, curr[1].right}); |
| 56 | + } |
| 57 | + } |
| 58 | + return t1; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +class Solution { |
| 64 | + public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { |
| 65 | + //If t1 == NULL -> t2 |
| 66 | + //If t2 == NULL -> t2 |
| 67 | + // Sum (t1,t2) |
| 68 | + |
| 69 | + if(t1 == null) return t2; |
| 70 | + if(t2 == null) return t1; |
| 71 | + |
| 72 | + Queue<TreeNode[]> q = new LinkedList<>(); |
| 73 | + q.add(new TreeNode[]{t1,t2}); |
| 74 | + |
| 75 | + //While is not empty process the nodes |
| 76 | + while(!q.isEmpty()) { |
| 77 | + TreeNode[] curr = q.remove(); |
| 78 | + if(curr[1] != null) { |
| 79 | + curr[0].val+=curr[1].val; |
| 80 | + |
| 81 | + //1. curr[0] = null -> curr[1] |
| 82 | + //2. curr[0] = null ->, add it queue |
| 83 | + |
| 84 | + //Left Tree |
| 85 | + if(curr[0].left == null) { |
| 86 | + curr[0].left = curr[1].left; |
| 87 | + } |
| 88 | + else { |
| 89 | + q.add(new TreeNode[]{curr[0].left, curr[1].left}); |
| 90 | + } |
| 91 | + |
| 92 | + //Right Tree |
| 93 | + if(curr[0].right == null) { |
| 94 | + curr[0].right = curr[1].right; |
| 95 | + } |
| 96 | + else { |
| 97 | + q.add(new TreeNode[]{curr[0].right, curr[1].right}); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return t1; |
| 102 | + } |
| 103 | +} |
0 commit comments