zoukankan      html  css  js  c++  java
  • 剑指Offer 27 二叉树的镜像

    二叉树的镜像

    操作给定的二叉树,将其变换为源二叉树的镜像。

     1 # -*- coding:utf-8 -*-
     2 # class TreeNode:
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.left = None
     6 #         self.right = None
     7 class Solution:
     8     # 返回镜像树的根节点
     9     def Mirror(self, root):
    10         if root == None:
    11             return None
    12         if root.left != None and root.right != None:
    13             temp = TreeNode(root.val)
    14             temp.left = root.left
    15             root.left = self.Mirror(root.right)
    16             root.right = self.Mirror(temp.left)
    17         elif root.left == None and root.right != None:
    18             root.left = self.Mirror(root.right)
    19             root.right = None
    20         elif root.right == None and root.left != None:
    21             root.right = self.Mirror(root.left)
    22             root.left = None
    23         return root
    24         # write code here
  • 相关阅读:
    codeforces 671C
    zoj3256
    hit2739
    hdu5737
    2017.1其他简要题解
    hdu5967
    初探插头dp
    11月下旬题解
    CompletableFuture的get和getNow()的区别
    python 镜像
  • 原文地址:https://www.cnblogs.com/asenyang/p/11013140.html
Copyright © 2011-2022 走看看