当前位置:网站首页>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
边栏推荐
- redis探索之缓存击穿、缓存雪崩、缓存穿透
- First intention is the most important
- ASP.NET Core 6 从入门到企业级实战开发应用技术汇总
- fatal error: execution: 没有那个文件或目录
- Wechat simulated geographical location_ Camouflage wechat location
- Efforts at the turn of the decade
- 队列的链式存储
- Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
- Double linked list related operations
- 双链表有关操作
猜你喜欢

用.Net Core接入微信公众号开发

Stack-------

Nc100 converts strings to integers (ATOI)

【20211129】Jupyter Notebook遠程服務器配置
![[20211129] configuration du serveur distant du carnet de notes jupyter](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] configuration du serveur distant du carnet de notes jupyter
![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/

STM32 project practice (1) introduction and use of photosensitive resistor

被锡膏坑了一把
![[20211129] jupyter notebook remote server configuration](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] jupyter notebook remote server configuration
![[datawhale202206] pytorch recommendation system: precision model deepfm & DIN](/img/4f/8799016731a2c1647b6f2f4d96b754.png)
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
随机推荐
Sort out relevant contents of ansible
【邂逅Django】——(二)数据库配置
使用BurpSuite对app抓包教程
项目部署,一点也不难!
ROS2 Foxy depthai_ros教程
List of QT players [easy to understand]
VS Code 设置代码自动保存
Teach you to complete the actual battle of image classification hand in hand -- Image Recognition Based on convolutional neural network
Eurake分区理解
下半年还有很多事要做
VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
华为面试题: 招聘
Chained storage of queues
硬阈值(Hard Thresholding)函数解读[通俗易懂]
ANSI/UL 94 VTM薄质材料垂直燃烧测试
Mobile note application
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
Ansible相关内容梳理
Operations related to sequence table
JS reverse | m3u8 data decryption of a spring and autumn network