zoukankan      html  css  js  c++  java
  • 100相同的树

    题目:给定两个二叉树,编写一个函数来检验它们是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

    来源: https://leetcode-cn.com/problems/same-tree/

    法一: 自己的代码

    思路: 一定要记住对判断树节点的值之前,要先判断节点是否为None,如果为None则其没有节点的三个属性,利用栈每次如果两对节点不为空,则压栈,继续判断

    # 执行用时 :28 ms, 在所有 python3 提交中击败了99.60% 的用户
    # 内存消耗 :12.8 MB, 在所有 python3 提交中击败了99.09%的用户
    # 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, q):
            stack = [(p, q)]
            while stack:
                a,b = stack.pop()
                # 注意空对象没有val,left,right这些属性,要先判断题他们是否为空
                # None是无法用 a.val == b.val进行判断的,
                # 若同时为空,继续判断, 用and的时候可以不加括号
                if a is None and b is None:
                # 用下面这个判断耗时会短些
                # if type(a) == type(b) == type(None):
                    pass
                # 有一个不为空,说明不一样,返回False
                elif a is None or b is None:
                    return False
                elif a.val == b.val:
                    # 如果相等,这把它们的两个子节点都压栈等待比较.
                    stack.append((a.left, b.left))
                    stack.append((a.right,b.right))
                else:
                    # 如果有一个val不相等,返回False
                    return False
            # 全执行完了没有返回False,说明一样返回True
            return True
    if __name__ == '__main__':
        duixiang = Solution()
        root1 = TreeNode(1)
        root1.left = TreeNode(2)
        root1.right = TreeNode(3)
        root2 = TreeNode(1)
        root2.left = TreeNode(2)
        root2.right = TreeNode(3)
        a = duixiang.isSameTree(root1, root2)
        print(a)
    View Code

    法二:官方代码, 利用递归,要先写好递归结束的条件,再写递归的开始,

    也可以用队列来实现,如果用队列的话,就是层次遍历了

    class Solution(object):
        def isSameTree(self, p, q):
            """
            :type p: TreeNode
            :type q: TreeNode
            :rtype: bool
            """
            # 如果p和q都为空就返回True
            if not (p or q):
                return True
            # 反之,如果p和q有一个不为空就返回False
            if not (p and q):
                return False
            # 如果p的值 != q的值,也返回False    
            if p.val!=q.val:
                return False
            # 递归比较p的左节点和q的左边点,再递归比较p的右节点和q的右节点    
            return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
    
    
    作者:user7439t
    链接:https://leetcode-cn.com/problems/same-tree/solution/dong-hua-yan-shi-100-xiang-tong-de-shu-by-user7439/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    View Code
  • 相关阅读:
    spring boot中 使用http请求
    spring boot 多层级mapper
    spring boot 使用拦截器,注解 实现 权限过滤
    spring boot 集成mybatis报错
    spring boot 使用拦截器 实现 用户登录拦截
    mac eclipse 删除不用的workspace
    maven+eclipse+mac+tomcat 多模块发布
    启动spring boot 异常
    安装 redis [standlone模式]
    quartz项目中的运用
  • 原文地址:https://www.cnblogs.com/xxswkl/p/11988781.html
Copyright © 2011-2022 走看看