题目来源:
https://leetcode.com/problems/symmetric-tree/
题意分析:
判断一棵树是不是镜像树。
题目思路:
判断左子树的左右子树是不是和右子树的右左子树相同。
代码(python):
# 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 """ def solve(p,q): if p == None: return q == None if q == None: return p == None if p.val == q.val: return solve(p.left,q.right) and solve(p.right,q.left) return False if root == None: return True else: return solve(root.left,root.right)