LEETCODE 101. 对称二叉树
题目描述
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
代码实现
递归版本
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if(not root): return True def recursion(n1,n2): if(not n1 and not n2): return True if(not n1 and n2) or (n1 and not n2): return False if(n1.val != n2.val): return False if recursion(n1.left,n2.right): return recursion(n1.right,n2.left) return False return recursion(root.left,root.right)4 次阅读