当前位置:网站首页>[dai6] mirror image of JZ27 binary tree
[dai6] mirror image of JZ27 binary tree
2022-07-07 10:29:00 【strawberry47】
subject :
Operate a given binary tree , Transform it into a mirror image of the source binary tree .
Ideas
What returns is a tree , That has to be established TreeNode Well , Thought of two methods :
① Using the queue , Save from right to left node, The order read out in this way is mirrored ; But the answer requires outputting a tree , I don't know how to convert it into a tree
② Using recursion , When children are leaf nodes , Exchange the positions of the left and right nodes ; But I still can't write ,o(╥﹏╥)o
Secretly read the answer , Use the stack to store nodes , Exchange left and right nodes every time you take them out , So I wrote the code according to this idea :
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
#
#
# @param pRoot TreeNode class
# @return TreeNode class
#
class Solution:
def Mirror(self , pRoot: TreeNode) -> TreeNode:
# write code here
if pRoot is None:
return None
q = []
q.append(pRoot)
while len(q):
for i in range(len(q)):
node = q.pop()
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
# swap
tmp = node.left
node.left = node.right
node.right = tmp
return pRoot
answer :
Look at recursion :
The problem solving steps :
1、 Special judgement : If pRoot It's empty , Returns an empty
2、 Exchange left and right subtrees
3、 hold pRoot Put the left subtree of Mirror Mirror in
4、 hold pRoot The right subtree of Mirror Mirror in
5、 Return root node root
class Solution:
def Mirror(self , pRoot ):
# write code here
if not pRoot:
return pRoot
# Left and right subtrees exchange
pRoot.left, pRoot.right = pRoot.right, pRoot.left
# Recursive left and right subtrees
self.Mirror(pRoot.left)
self.Mirror(pRoot.right)
return pRoot
I searched the Internet for a recursive solution :
To solve the problem of recursion is usually a three-step process , Namely :
First step , Define function function -> Flip the tree
The second step , Looking for recursive termination conditions -> It is already a leaf node or It's already empty
The second step , The equivalent relation of recursive function -> Flip node The left and right subtrees of
边栏推荐
- 原型与原型链
- IO模型复习
- BigDecimal数值比较
- Guid primary key
- Talking about the return format in the log, encapsulation format handling, exception handling
- STM32 product introduction
- JMeter installation
- Smart city construction based on GIS 3D visualization technology
- Fiddler break point
- When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types
猜你喜欢

5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!

LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件

Postman interface test III

Some properties of leetcode139 Yang Hui triangle

Use of JSON extractor originals in JMeter

LeetCode 练习——113. 路径总和 II

Yarn的基础介绍以及job的提交流程

OpenGL glLightfv 函数的应用以及光源的相关知识

Methods of adding centerlines and centerlines in SolidWorks drawings

JMeter installation
随机推荐
OpenGL glLightfv 函数的应用以及光源的相关知识
Multisim--软件相关使用技巧
ThreadLocal会用可不够
移动端通过设置rem使页面内容及字体大小自动调整
fiddler-AutoResponder
Inno setup packaging and signing Guide
1324:【例6.6】整数区间
Guide de signature du Code Appx
P2788 数学1(math1)- 加减算式
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
SQLyog数据库怎么取消自动保存更改
Jump to the mobile terminal page or PC terminal page according to the device information
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
gym安装踩坑记录
关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
Why is the reflection efficiency low?
Remote meter reading, switching on and off operation command
IO model review
JMeter about setting thread group and time
Download Text, pictures and ab packages used by unitywebrequest Foundation