-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeTrees.py
59 lines (42 loc) · 1.38 KB
/
mergeTrees.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : mergeTrees.py
@Time : 2024/10/12 21:11:29
@Author : Zihao Zheng
@Email : [email protected]
'''
from collections import deque
class Solution():
def mergeTree(self,root1,root2):
if not root1:
return root2
if not root2:
return root1
root1.val=root1.val+root2.val
root1.left=self.mergeTree(root1.left,root2.left)
root1.right=self.mergeTree(root1.right,root2.right)
return root1
def mergeTree(self,root1,root2):
if not root1:
return root2
if not root2:
return root1
queue=deque()
queue=deque([root1])
queue=deque([root2])
while queue:
node1=queue.popleft()
node2=queue.popleft()
if node1.left and node2.left:
queue.append(node1.left)
queue.append(node2.lfet)
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