当前位置:网站首页>leetcode 二叉树类
leetcode 二叉树类
2022-07-28 17:05:00 【Yuki_1999】
114题:二叉树前序遍历
迭代法+栈,实现二叉树的前序遍历
class Solution(object):
def preorderTraversal(self, root):
#栈+迭代法实现
#压栈append,出栈pop
if not root:
return []
stack=[root]
result=[]
while stack:
node=stack.pop()#处理中结点
result.append(node.val)
#先右再左,出栈先左后右
if node.right!=None:
stack.append(node.right)
if node.left!=None:
stack.append(node.left)
return result
226题:翻转二叉树
递归三部曲
1、确定递归函数的参数和返回值
2、确定终止条件
3、确定单层递归的逻辑
把每个孩子的左右结点翻转一下即可
class Solution(object):
def invertTree(self, root):
if not root:
return None
root.left,root.right=root.right,root.left
self.invertTree(root.left)
self.invertTree(root.right)
return root
边栏推荐
- It is said that software testing is the worst in the IT industry. Is that so?
- iptables 配置
- UE5 GAS 学习笔记 1.2游戏标签
- Cout.write learning
- Golang并发模型之
- Zen project management software is an indispensable tool for agile development teams
- .net WCF wf4.5 state machine, bookmark and persistence
- UE5 GAS 学习笔记 1.6 技能Gameplay Ability
- MongoDB初始化操作
- MongoDB创建索引
猜你喜欢

天线的主要参数介绍

Application of Internet of things in smart city

Calibration of vector network analyzer (vector network)

Principle, classification and requirements of antenna

一文简述:SRv6基本原理

It is said that software testing is the worst in the IT industry. Is that so?

#夏日挑战赛#【FFH】JS自定义组件:DIY一个随点随用的键盘!(一)

What role does low code play in the digital transformation?

Go并发详解之一

顿悟!百度强推的Redis天花板笔记,原来数据库是这样理解的
随机推荐
连线:谁拥有未来的艺术?OpenAI允许Dall-E用户将作品商用化,目前而言
LeetCode79题 方法一:深度搜索
MySQL operation Encyclopedia
Brief introduction: basic principle of srv6
What role does low code play in the digital transformation?
一文简述:SRv6基本原理
频谱分析仪的性能参数
Mqtt over quic: the next generation Internet of things standard protocol injects new impetus into the message transmission scenario
MySQL advanced mvcc (ultra detailed collation)
腾讯汤道生:开源是产业互联网时代新的生产方式和协作模式
明德生物:公司暂未有产品被列入WHO推荐清单
Modifier modifier modifier of solidity _;
DC simulation example of ADS simulation
Go的walk库报错
DC-DC开关电源
Golang 并发之锁
NDK 系列(5):JNI 从入门到实践,爆肝万字详解!
Digital torrent: resource reorganization and strategic conflict in enterprise transformation
Iptables firewall port rule configuration
Golang 打包发布到各个平台