Leetcode 笔记 101 - Symmetric Tree

题目链接:Symmetric Tree | LeetCode OJ

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following is not:

    1
   / \
  2   2
   \   \
   3    3

Note:
Bonus points if you could solve it both recursively and iteratively.

Tags: Deep-first Search

分析

判断一个二叉树是否是镜像的条件是根节点的左右子树互为镜像,左右子树互为镜像的条件是左右子结点的内侧、外侧两个子树互为镜像,这本质上是一个递归问题。判断二叉树镜像的条件为:

  • 对称结点的值相等
  • 对称结点中左结点的左子树和右结点右子树互为镜像、左结点的右子树和右结点的左子树互为镜像

由于算法本身是递归的,因此只需要加入一个栈,每次把对称位置的结点压入栈内就可以改写成迭代的

示例

# Recursively Solution
class Solution:
  # @param root, a tree node
  # @return a boolean
  def isSymmetric(self, root):
    if root is None:
      return True
    else:
      return self.isMirror(root.left, root.right)

  def isMirror(self, left, right):
    if left is None and right is None:
      return True
    if left is None or right is None:
      return False

    if left.val == right.val:
      outPair = self.isMirror(left.left, right.right)
      inPiar = self.isMirror(left.right, right.left)
      return outPair and inPiar
    else:
      return False

# Iteratively Solution
class Solution:
  def isSymmetric(self, root):
    if root is None:
      return True

    stack = [[root.left, root.right]]

    while len(stack) > 0:
      pair = stack.pop()
      left = pair[0]
      right = pair[1]

      if left is None and right is None:
        continue
      if left is None or right is None:
        return False
      if left.val == right.val:
        stack.append([left.left, right.right])
        stack.append([left.right, right.left])
      else:
        return False
    return True

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

优化/扩展

  • 在判断外侧一对子树不符合镜像之后,可以立即返回False结果,而不需要再验证内侧一对子树是否符合要求。可以在部分情况下提高效率
posted @   前端兔子喵  阅读(944)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示