—Easy
https://leetcode.com/problems/merge-two-binary-trees/
Code:
class TreeNode(object): def __init__(self,x): self.val = x self.left = None self.right = None class Solution(object): def mergeTrees(self,t1:TreeNode,t2:TreeNode): if t1 is None and t2 is None: return if t1 is None: return t2 if t2 is None: return t1 t1.val += t2.val t1.right = self.mergeTrees(t1.right,t2.right) t1.left = self.mergeTrees(t1.left,t2.left) return t1
思路:
1.一开始没想明白到底是递归好还是迭代好,然后就没想明白,感觉下来还是递归好写但是递归的逻辑不易想明白
2.easy还是easy的一开始觉得迭代太麻烦,然后递归没想没明白如何让两个树一起递归,实际上感觉这个递归更有“伴随”的意味
3.好久没写了动起来啊