Leetcode 100. 相同的树 (每日一题 20210811)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 100. 相同的树 (每日一题 20210811)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
給你兩棵二叉樹的根節(jié)點 p 和 q ,編寫一個函數(shù)來檢驗這兩棵樹是否相同。如果兩個樹在結(jié)構(gòu)上相同,并且節(jié)點具有相同的值,則認為它們是相同的。示例 1:輸入:p = [1,2,3], q = [1,2,3]
輸出:true
示例 2:輸入:p = [1,2], q = [1,null,2]
輸出:false
示例 3:輸入:p = [1,2,1], q = [1,1,2]
輸出:false鏈接:https://leetcode-cn.com/problems/same-treeclass Solution:def isSameTree(self, p:TreeNode, q:TreeNode)->bool:if p is None and q is not None:return Falseif p is not None and q is None:return Falseif p is None and q is None:return Trueif p.val != q.val:return Falsereturn self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
總結(jié)
以上是生活随笔為你收集整理的Leetcode 100. 相同的树 (每日一题 20210811)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 98. 验证二叉搜索树
- 下一篇: Leetcode 344. 反转字符串