二叉树的镜像

2021-01-02  本文已影响0人  九日火

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

package main


import "fmt"


type TreeNode struct {
    Val    int
    Left   *TreeNode
    Right  *TreeNode
}

func FindMirrorTree(p *TreeNode) {
    if p == nil { return }
    p.Left, p.Right = p.Right, p.Left
    FindMirrorTree(p.Left)
    FindMirrorTree(p.Right)
}
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.right = None
        self.left = None


class Solution:
    def MirrorTree(self, root):
        if root == None:
            return None
        if root.left == None and root.right == None:
            return root
        root.left, root.right = root.right, root.left
        self.MirrorTree(root.left)
        self.MirrorTree(root.right)
上一篇 下一篇

猜你喜欢

热点阅读