当前位置:网站首页>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
边栏推荐
- leetcode 二叉树类
- UE5 GAS 学习笔记 1.9 技能系统全局类(AbilitySystemGlobals)
- Details of iptables firewall
- 实验楼----PHP大法
- Golang concurrent lock
- Error 2003 (HY000) can't connect to MySQL server on 'localhost3306' (10061) solution
- MYSQL入门与进阶(十)
- Msg.value of solidity
- What is the future of software testing?
- Ue5 gas learning notes 1.7 task ability tasks
猜你喜欢

1.1. Sparse array

npm 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

1.3 linked list

When golang encounters high concurrency seckill

MYSQL入门与进阶(四)

@Autowired与@Resource区别

Mqtt over quic: the next generation Internet of things standard protocol injects new impetus into the message transmission scenario

1.2 queue

EasyNLP中文文图生成模型带你秒变艺术家

腾讯汤道生:开源是产业互联网时代新的生产方式和协作模式
随机推荐
Introduction and advanced MySQL (4)
Examples of AC simulation and S-parameter simulation of ADS simulation
First understanding of structure
The required error of solidity is reported
2022.7.26 constructor, interview: the role of new, deep copy and shallow copy
VSC writes go, expected 'package' appears, found 'EOF‘
[GXYCTF2019]StrongestMind
Performance parameters of spectrum analyzer
Look at Devops construction from SRE
苹果开发完整的苹果证书与描述文件创建流程
Six countries in Europe and the United States launched an express line product to optimize the "end-to-end" performance service on the 5th
Ue5 gas learning notes 0.2 configuration plug-in
MYSQL入门与进阶(九)
Go concurrency one
Random talk on GIS data (VI) - projection coordinate system
2022-07-27 第四小组 修身课 学习笔记(every day)
Implementation of solid transfer function (based on transfer)
Ue5 gas learning notes 1.9 skill system global classes (abilitysystemglobals)
Mingde biology: no products of the company have been listed in the WHO recommended list
UE5 GAS 学习笔记 1.3属性Attribute