zoukankan      html  css  js  c++  java
  • Leetcode 笔记 100

    题目链接:Same Tree | LeetCode OJ

    Given two binary trees, write a function to check if they are equal or not.

    Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

    Tags: Depth-first Search

    分析

    很基本的一道深度优先遍历的题,没有太多好解释的,唯一需要注意的是leetcode约定的对结点为空的两个约定:

    • left, right指向None表示没有叶子结点
    • root不为None时(即结点存在),root.val不为None

    示例

    class Solution:
        # @param p, a tree node
        # @param q, a tree node
        # @return a boolean
        def isSameTree(self, p, q):
          if p is None and q is None:
            return True
          if p is None or q is None or p.val != q.val:
            return False
          return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
    

    Leetcode 笔记系列的Python代码共享在https://github.com/wizcabbit/leetcode.solution

    优化/简化

    题目很简单,优化和简化都不会构成数量级上的影响,计算的时空复杂度都是确定的。

    转载本博客站点(http://www.cnblogs.com/wizcabbit/)内的所有原创内容时,必须遵循此协议:

    署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0)

    禁止对文章内容做出更改,禁止的行为包括但不限于:修改内容、修改图片、去除链接、去除作者标识

    必须在转载中标识本文的显式链接,且链接必须可以点击

    遵守CC协议是为了更好地保持原创内容的质量,保留针对协议中的主张进行追诉的权利。

  • 相关阅读:
    Multidimensional Arrays
    TortoiseGit
    Excel教程(14)
    Excel教程(13)
    Excel教程(12)
    Excel教程(11)
    lock
    reflect
    game
    html x
  • 原文地址:https://www.cnblogs.com/wizcabbit/p/leetcode-100-same-tree.html
Copyright © 2011-2022 走看看