一道相似的leetcode题:判断是否为对称的树https://leetcode-cn.com/problems/symmetric-tree/comments/
class Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ def check(node1, node2): if not node1 and not node2: return True elif not node1 or not node2: return False if node1.val != node2.val: return False return check(node1.left, node2.right) and check(node1.right, node2.left) return check(root, root)
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # 返回镜像树的根节点 def Mirror(self, root): # write code here if not root: return if not root.left and not root.right: return root.left, root.right = root.right, root.left if root.left:self.Mirror(root.left) if root.right:self.Mirror(root.right) return root