当前位置:网站首页>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
边栏推荐
- 手机便签应用
- [shell programming] - shell introductory learning
- 腾讯总考epoll, 很烦
- 数字信号处理——线性相位型(Ⅱ、Ⅳ型)FIR滤波器设计(2)
- 队列操作---
- Relationship between accuracy factor (DOP) and covariance in GPS data (reference link)
- 79. 单词搜索【dfs + 回溯visit + 遍历起点】
- codeforces -- 4B. Before an Exam
- 基因检测,如何帮助患者对抗疾病?
- [datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
猜你喜欢
![[speech signal processing] 3 speech signal visualization -- prosody](/img/06/5f57f9dfe3a0f2f70022706f7d4d17.jpg)
[speech signal processing] 3 speech signal visualization -- prosody

《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现

leetcode:241. 为运算表达式设计优先级【dfs + eval】

I wish you all a happy reunion
![[20220605] Literature Translation -- visualization in virtual reality: a systematic review](/img/11/6c42957186bf530e8f9d4025a40197.png)
[20220605] Literature Translation -- visualization in virtual reality: a systematic review

BIM and safety in road maintenance-buildSmart Spain

【脑洞大开】《西潮》及《走向世界丛书》

2022-06-28-06-29

第十四章 信号(四)- 多进程任务示例

79. 单词搜索【dfs + 回溯visit + 遍历起点】
随机推荐
leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE
JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
6.30模拟赛总结
腾讯黎巍:深耕“监管科技”,护航数字经济行稳致远
手机便签应用
栈-------
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/
微信模拟地理位置_伪装微信地理位置
[106] 360 check font - check whether the copyright of local Fonts is commercially available
Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.
AI抠图工具
Operations related to sequence table
MySQL common functions
華為面試題: 招聘
GPS 数据中的精度因子(DOP)与协方差之间的关系 (参考链接)
Huawei interview question: Recruitment
[Maui] add click events for label, image and other controls
木架的场景功能
Using burpsuite to capture app packages