首页 > 其他 > 详细

leetcode 每日一题 100.相同的树

时间:2020-06-24 13:27:07      阅读:68      评论:0      收藏:0      [点我收藏+]

技术分享图片

技术分享图片

递归

思路:

递归判断根节点,左子树,右子树是否相同。

代码:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not p and not q:
            return True
        if not q or not p:
            return False
        if p.val != q.val:
            return False
        return self.isSameTree(p.right, q.right) and                self.isSameTree(p.left, q.left)

 

leetcode 每日一题 100.相同的树

原文:https://www.cnblogs.com/nilhxzcode/p/13186761.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!