美文网首页
Day49:是否为对称二叉树

Day49:是否为对称二叉树

作者: 快乐的老周 | 来源:发表于2020-07-21 11:56 被阅读0次

def is_Symmetic(root):
'''
Day49:是否为对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1 1 2^0 - 1
/
2 2 1 2^1 - 1
/ \ /
3 4 4 3 2 2^2
/ \ / \ / \ /
5 6 7 8 8 7 6 5
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/
2 2
\
3 3
来源:力扣(LeetCode) https://leetcode-cn.com/problems/symmetric-tree
'''
if not root:
return True
def sub(left, right):
if not left and not right:
return True
if not left or not right :
return False
return left.val == right.val and sub(left.left,right.right) and sub(left.right, right.left)
return sub(root.left, root.right)

相关文章

  • Day49:是否为对称二叉树

    def is_Symmetic(root):'''Day49:是否为对称二叉树给定一个二叉树,检查它是否是镜像对称...

  • 第九天的leetcode刷题

    今天的题目是判断是否为对称二叉树:101. 对称二叉树[https://leetcode-cn.com/probl...

  • 二叉树的层次遍历要先掌握 有一些题目是相似的比如:求二叉树的深度和是否为平衡二叉树;是否是相同的二叉树,是否为对称...

  • 101. Symmetric Tree

    判断二叉树是否对称 同时遍历左子树和右子树,判断是否对称

  • [LeetCode OJ]- SymmetricTree

    题目要求:判断一颗二叉树是否为左右对称的。这里的左右对称不仅要求结构上左右对称,而且节点的值也应该满足左右对称。 ...

  • 【LeetCode】101-对称二叉树

    对称二叉树 题目 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的...

  • 26.对称二叉树

    判断一个二叉树是否为对称二叉树。对称二叉树的定义是:一个树的镜像和本身相同。 分析:对二叉树进行前序遍历,和一种特...

  • 二叉树的相似、镜像问题

    二叉树的镜像: 100.Same Tree(二叉树是否相同) 101.Symmetric Tree(二叉树是否对称)

  • 2019-04-09 BFS广度优先搜索刷题Day1

    Leetcode 101 对称二叉树 题目描述: 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2...

  • Leetcode.101.Symmetric Tree

    题目 给定一个二叉树, 判断这个二叉树是否对称. 思路 判断这个数是否对称: 将根节点的右边子树所有左右节点都交换...

网友评论

      本文标题:Day49:是否为对称二叉树

      本文链接:https://www.haomeiwen.com/subject/hmubkktx.html