2018-12-29

LeetCode 101. Symmetric Tree.jpg

LeetCode 101. Symmetric Tree

Description

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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

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

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3

描述

  • 给定一棵二叉树,判断此二叉树是否左右对称.

思路

  • 树的题目使用递归求解的情况比较多,也相对比较简单.
  • 令给定的树的左子树为p,右子树为p,对称的意思是p的左子树和q的右子树相等,p的右子树和q的左子树相等.
# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2018-12-29 09:30:27
# @Last Modified by:   何睿
# @Last Modified time: 2018-12-29 09:42:54

# Definition for a binary tree node.


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        # 如果为空树,返回ture
        if not root:
            return True
        return self.recursion(root.left, root.right)

    def recursion(self, p, q):
        # 如果两个子树都为空,返回True
        if p == None and q == None:
            return True
        # 如果有一个子树为空,另一个子树不为空,返回False
        if (p == None and q != None) or (p != None and q == None):
            return False
        # 能够到达这里,说明两个子树都不为空,如果值不相等,返回False
        if p.val != q.val:
            return False
        # 判断当前子树p的左子树与子树q的右子树是否为镜像子树
        leftree = self.recursion(p.left, q.right)
        # 判断当前子树p的右子树与子树q的左子树是否为镜像子树
        rightree = self.recursion(p.right, q.left)
        return leftree and rightree

源代码文件在这里.
©本文首发于何睿的博客,欢迎转载,转载需保留文章来源,作者信息和本声明.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容