Iteration solution
In order to merge the 2 binary trees, we need to traverse them in the same pace.
This traverse can be done in BFS or DFS style.
We do it in BFS in this solution.
It is obvious that the key problem here is to maintain the same order of traversal of the 2 trees. And we only want to traverse those nodes that t1 and t2 both have.
For those nodes that only one of them has, simply put the nodes and the entire subtree of the nodes in the corresponding position in the result.
In order to save space, we use t1 as the result, meaning we will modify the nodes in t1 based on traversal status.
The algorithm is showed as following:
- Put root of
t1andt2into queue. - Take one node out from
queue1andqueue2ascurr1andcurr2respectively. Sum up theval(since this is the node that botht1andt2have) as thevalof the new node at this position. - Check for the
left childofcurr1andcurr2.
- If both of them have
left child, putleft childinto the 2queuesrespectively. - If only one of them has
left childor both have noleft child, set current result node’sleft childas theleft childthat is notnullornull, respectively.
- If both of them have
- Check for the
right childofcurr1andcurr2. Do the same checking as step3 above. - Check for if the 2
queueis both empty. If so, stop the loop. Otherwise continue.
The time complexity of this algorithm is O(h) where h is the depth of the deepest common nodes t1 and t2 have.
The space complexity of this O(n) where n is the number of the common nodes of t1 and t2.
Code is showed as following:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null) return t2;
if (t2 == null) return t1;
Deque<TreeNode> deque1 = new ArrayDeque<>();
Deque<TreeNode> deque2 = new ArrayDeque<>();
// BFS
deque1.offer(t1);
deque2.offer(t2);
// Using t1 as the resulting Binary Tree
while (!deque1.isEmpty() || !deque2.isEmpty()) {
TreeNode curr1 = deque1.poll();
TreeNode curr2 = deque2.poll();
TreeNode result = curr1;
result.val = curr1.val + curr2.val;
if (curr1.left == null || curr2.left == null) {
result.left = curr1.left == null ? curr2.left : curr1.left;
} else if (curr1.left != null && curr2.left != null) {
deque1.offer(curr1.left);
deque2.offer(curr2.left);
}
if (curr1.right == null || curr2.right == null) {
result.right = curr1.right == null ? curr2.right : curr1.right;
} else if (curr1.right != null && curr2.right != null) {
deque1.offer(curr1.right);
deque2.offer(curr2.right);
}
}
return t1;
}
}
Recursion solution
Considering the recursion feature of Binary Tree, using recursion to solve this question is straightforward.
If both current TreeNode of t1 and t2 is not null, set the result tree’s current node val as t1.val + t2.val.
Otherwise return the not null one or null.
Recursively do this for t1 and t2’s left and right child.
Time and space complexity is the same as iteration.
public class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null) return t2;
if (t2 == null) return t1;
t1.val += t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
}
}

本文介绍了一种将两棵二叉树合并的算法,通过迭代和递归两种方式实现。迭代方法采用广度优先搜索(BFS),使用队列同步遍历两棵树,并更新节点值。递归方法则直接对两棵树进行比较和合并。两种方法的时间复杂度均为O(h),空间复杂度为O(n),其中h为最深公共节点深度,n为共同节点数。
352

被折叠的 条评论
为什么被折叠?



