当前位置:网站首页>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
边栏推荐
- GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
- 哪个券商公司开户佣金低又安全又可靠
- 【20211129】Jupyter Notebook遠程服務器配置
- 腾讯总考epoll, 很烦
- ANSI/UL 94 VTM薄质材料垂直燃烧测试
- Topic 1004: the story of cows (recursion)
- VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
- Queue operation---
- Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
- Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
猜你喜欢

Sleep quality today 79 points

双链表有关操作

队列的链式存储
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]

JS reverse | m3u8 data decryption of a spring and autumn network

Nc100 converts strings to integers (ATOI)
![[brain opening] west tide and going to the world series](/img/b2/444af296e170d19629800b3d4c50fa.jpg)
[brain opening] west tide and going to the world series

Four years after graduation: work, resign, get married, buy a house

VS Code 设置代码自动保存

Circular linked list--
随机推荐
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
Efforts at the turn of the decade
Leetcode (Sword finger offer) - 58 - ii Rotate string left
循环链表--
R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
[20211129] configuration du serveur distant du carnet de notes jupyter
codeforces -- 4B. Before an Exam
[speech signal processing] 3 speech signal visualization -- prosody
手机便签应用
Chain storage of binary tree
腾讯黎巍:深耕“监管科技”,护航数字经济行稳致远
Mobile note application
【脑洞大开】《西潮》及《走向世界丛书》
ASP.NET Core 6 从入门到企业级实战开发应用技术汇总
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/
Chained storage of queues
Sleep quality today 79 points
JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告