3/100. Merge Two Binary Trees
生活随笔
收集整理的這篇文章主要介紹了
3/100. Merge Two Binary Trees
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
將兩個二叉樹相同位置的數值相加,相加的方法一樣,則使用遞歸法挨個相加即可。
更優寫法:
t1 and t1.left
def mergeTrees(self, t1, t2):if not t1 and not t2: return Noneans = TreeNode((t1.val if t1 else 0) + (t2.val if t2 else 0))ans.left = self.mergeTrees(t1 and t1.left, t2 and t2.left)ans.right = self.mergeTrees(t1 and t1.right, t2 and t2.right)return ans總結
以上是生活随笔為你收集整理的3/100. Merge Two Binary Trees的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2/100. Hamming Dista
- 下一篇: 4/100. Maximum Depth