27. 二叉树的镜像

二叉树的镜像

http://www.nowcoder.com/questionTerminal/564f4c26aa584921bc75623e48ca3011

一道相似的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
全部评论

相关推荐

这个简历还有救吗,考研失利了,完蛋蛋了
飞屋一号:放宽心,9爷就业还是轻轻松松的
点赞 评论 收藏
分享
02-04 17:01
南昌大学 Java
牛客96763241...:拿插件直接投就完了,这玩意看运气的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务