当前位置:网站首页>leetcode:226. Flip binary tree [DFS flip]
leetcode:226. Flip binary tree [DFS flip]
2022-07-01 12:36:00 【White speed Dragon King's review】

analysis
dfs Return the result of this subtree inversion
If it is a leaf node, it will return to itself
If there's a left child , The right child becomes the reverse left child
If there is a right child , The left child becomes the right child after reversal
Then we need to use it. temp Record it in the middle , Can't change sides dfs
temp The initial value is None that will do
ac code
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
if not root:
return root
def dfs(node):
if node.left is None and node.right is None:
return node
temp1 = temp2 = None
if node.left:
temp1 = dfs(node.left)
if node.right:
temp2 = dfs(node.right)
node.right = temp1
node.left = temp2
return node
dfs(root)
return root
summary
Reverse binary tree used dfs It is also a classic question
边栏推荐
- 关于NAND FLASH解扣的认识
- (mixed version 1) multiple TXT text to one table
- Wechat applet - 80 practical examples of wechat applet projects
- 队列操作---
- 第十四章 信号(四)- 多进程任务示例
- 项目部署,一点也不难!
- logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable
- 【语音信号处理】3语音信号可视化——prosody
- Friends day 2022
- kubernetes之ingress探索实践
猜你喜欢
![Wechat applet reports an error: [rendering layer network layer error] pages/main/main Local resource pictures in wxss cannot be obtained through wxss. You can use network pictures, Base64, or < image/](/img/6a/fe448ca635690bc5260436546b588e.jpg)
Wechat applet reports an error: [rendering layer network layer error] pages/main/main Local resource pictures in wxss cannot be obtained through wxss. You can use network pictures, Base64, or < image/

Queue operation---

晓看天色暮看云,美图欣赏

栈-------
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]

数论基础及其代码实现

Nc100 converts strings to integers (ATOI)

leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】

ROS2 Foxy depthai_ros教程

Four years after graduation: work, resign, get married, buy a house
随机推荐
[shell programming] - shell introductory learning
6.30模拟赛总结
[JS advanced] promise explanation
Share several tools for designing exquisite circuit diagrams
木架的场景功能
Exploration and practice of inress in kubernetes
二叉树的链式存储
工具箱之 IKVM.NET 项目新进展
fatal error: execution: 没有那个文件或目录
R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型在每个交叉验证(或者重采样)的每一折fold上的混淆矩阵、并使用summary输出每个fold的其它详细指标
网络socket的状态要怎么统计?
Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
第十四章 信号(四)- 多进程任务示例
How to use opcache, an optimization acceleration component of PHP
[106] 360 check font - check whether the copyright of local Fonts is commercially available
天青色等烟雨
腾讯总考epoll, 很烦
ASP.NET Core 6 从入门到企业级实战开发应用技术汇总
Ipv6-6to4 experiment