-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmerge_two_binary_trees.py
More file actions
41 lines (28 loc) · 1001 Bytes
/
merge_two_binary_trees.py
File metadata and controls
41 lines (28 loc) · 1001 Bytes
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
# 617. Merge Two Binary Trees
# https://leetcode.com/problems/merge-two-binary-trees/
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def dfs(self, t1, t2):
t1.val += t2.val
if t1.left is not None and t2.left is not None:
self.mergeTrees(t1.left, t2.left)
if t1.right is not None and t2.right is not None:
self.mergeTrees(t1.right, t2.right)
if t1.left is None and t2.left is not None:
t1.left = t2.left
if t1.right is None and t2.right is not None:
t1.right = t2.right
return t1
def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode:
if t1 is None and t2 is None:
return None
if t1 is not None and t2 is None:
return t1
if t1 is None and t2 is not None:
return t2
return self.dfs(t1, t2)