当前位置:网站首页>Leetcode binary tree class
Leetcode binary tree class
2022-07-28 18:44:00 【Yuki_ one thousand nine hundred and ninety-nine】
114 topic : Binary tree preorder traversal
Iterative method + Stack , Binary tree to achieve the preorder traversal
class Solution(object):
def preorderTraversal(self, root):
# Stack + Iterative implementation
# Pressing stack append, Out of the stack pop
if not root:
return []
stack=[root]
result=[]
while stack:
node=stack.pop()# Processing node
result.append(node.val)
# Right then left , Leave the stack first and then right
if node.right!=None:
stack.append(node.right)
if node.left!=None:
stack.append(node.left)
return result
226 topic : Flip binary tree
Recursive Trilogy
1、 Determine the parameters and return values of recursive functions
2、 Determine termination conditions
3、 Determine the logic of monolayer recursion
Just flip the left and right nodes of each child
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
边栏推荐
- Software testing needs more and more talents, but fewer people are on the road of testing?
- leetcode 二叉树类
- MYSQL入门与进阶(三)
- LeetCode_96_不同的二叉搜索树
- UE5 GAS 学习笔记 1.9 技能系统全局类(AbilitySystemGlobals)
- #夏日挑战赛#【FFH】JS自定义组件:DIY一个随点随用的键盘!(一)
- Modifier modifier modifier of solidity _;
- Cout.write learning
- UE5 GAS 学习笔记 1.3属性Attribute
- How to see the future development of software testing?
猜你喜欢
![Summer Challenge [FFH] JS custom component: DIY a keyboard that can be used at any time! (I)](/img/92/abc5c4e7aee3c65aa0f62fc699b4df.jpg)
Summer Challenge [FFH] JS custom component: DIY a keyboard that can be used at any time! (I)

Gateway入门

Introduction to oscilloscope

When golang encounters high concurrency seckill

MySQL advanced mvcc (ultra detailed collation)

三分钟了解快来新媒体

Bubble sorting and Related videos

DC-DC switching power supply

高德地图实现自定义小蓝点 自定义点标记 绘制多边形/圆形区域 根据地图的移动显示或者隐藏自定义点标记的相关实现

Introduction and advanced level of MySQL (8)
随机推荐
2022.7.26 constructor, interview: the role of new, deep copy and shallow copy
MYSQL入门与进阶(八)
Introduction and advanced MySQL (4)
2022.7.26 构造函数,面试:new的作用、深拷贝和浅拷贝
Calibration of vector network analyzer (vector network)
First understanding of structure
Introduction to main parameters of antenna
十进制转二进制进阶版(可转化负数以及边界值)
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
Record your interview experience in Xiamen for two years -- Conclusion
Ue5 gas learning notes 1.7 task ability tasks
UE5 GAS 学习笔记 1.8 游戏特效(GameplayCue)
UE5 GAS 学习笔记 1.9 技能系统全局类(AbilitySystemGlobals)
MongoDB初始化
Ue5 gas learning notes 1.9 skill system global classes (abilitysystemglobals)
Brief introduction to the principle of spectrometer II
UE5 GAS 学习笔记 1.10 预测(Prediction)
UE5 GAS 学习笔记 1.7 任务Ability Tasks
1.3 linked list
Cout.write learning