mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #545 from KelvinG-611/617.合并二叉树
Update 0617.合并二叉树.md
This commit is contained in:
@ -312,6 +312,8 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
**递归法 - 前序遍历**
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode:
|
# class TreeNode:
|
||||||
@ -319,41 +321,57 @@ Python:
|
|||||||
# self.val = val
|
# self.val = val
|
||||||
# self.left = left
|
# self.left = left
|
||||||
# self.right = right
|
# self.right = right
|
||||||
# 递归法*前序遍历
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
|
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
|
||||||
if not root1: return root2 // 如果t1为空,合并之后就应该是t2
|
# 递归终止条件:
|
||||||
if not root2: return root1 // 如果t2为空,合并之后就应该是t1
|
# 但凡有一个节点为空, 就立刻返回另外一个. 如果另外一个也为None就直接返回None.
|
||||||
root1.val = root1.val + root2.val //中
|
if not root1:
|
||||||
root1.left = self.mergeTrees(root1.left , root2.left) //左
|
return root2
|
||||||
root1.right = self.mergeTrees(root1.right , root2.right) //右
|
if not root2:
|
||||||
return root1 //root1修改了结构和数值
|
return root1
|
||||||
|
# 上面的递归终止条件保证了代码执行到这里root1, root2都非空.
|
||||||
|
root1.val += root2.val # 中
|
||||||
|
root1.left = self.mergeTrees(root1.left, root2.left) #左
|
||||||
|
root1.right = self.mergeTrees(root1.right, root2.right) # 右
|
||||||
|
|
||||||
# 迭代法-覆盖原来的树
|
return root1 # ⚠️ 注意: 本题我们重复使用了题目给出的节点而不是创建新节点. 节省时间, 空间.
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
**迭代法**
|
||||||
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
|
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
|
||||||
if not root1: return root2
|
if not root1:
|
||||||
if not root2: return root1
|
return root2
|
||||||
# 迭代,将树2覆盖到树1
|
if not root2:
|
||||||
queue1 = [root1]
|
return root1
|
||||||
queue2 = [root2]
|
|
||||||
root = root1
|
|
||||||
while queue1 and queue2:
|
|
||||||
root1 = queue1.pop(0)
|
|
||||||
root2 = queue2.pop(0)
|
|
||||||
root1.val += root2.val
|
|
||||||
if not root1.left: # 如果树1左儿子不存在,则覆盖后树1的左儿子为树2的左儿子
|
|
||||||
root1.left = root2.left
|
|
||||||
elif root1.left and root2.left:
|
|
||||||
queue1.append(root1.left)
|
|
||||||
queue2.append(root2.left)
|
|
||||||
|
|
||||||
if not root1.right: # 同理,处理右儿子
|
queue = deque()
|
||||||
root1.right = root2.right
|
queue.append(root1)
|
||||||
elif root1.right and root2.right:
|
queue.append(root2)
|
||||||
queue1.append(root1.right)
|
|
||||||
queue2.append(root2.right)
|
while queue:
|
||||||
return root
|
node1 = queue.popleft()
|
||||||
|
node2 = queue.popleft()
|
||||||
|
# 更新queue
|
||||||
|
# 只有两个节点都有左节点时, 再往queue里面放.
|
||||||
|
if node1.left and node2.left:
|
||||||
|
queue.append(node1.left)
|
||||||
|
queue.append(node2.left)
|
||||||
|
# 只有两个节点都有右节点时, 再往queue里面放.
|
||||||
|
if node1.right and node2.right:
|
||||||
|
queue.append(node1.right)
|
||||||
|
queue.append(node2.right)
|
||||||
|
|
||||||
|
# 更新当前节点. 同时改变当前节点的左右孩子.
|
||||||
|
node1.val += node2.val
|
||||||
|
if not node1.left and node2.left:
|
||||||
|
node1.left = node2.left
|
||||||
|
if not node1.right and node2.right:
|
||||||
|
node1.right = node2.right
|
||||||
|
|
||||||
|
return root1
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
Reference in New Issue
Block a user