当前位置:网站首页>Leetcode - 226. Retourner l'arbre binaire (bfs)
Leetcode - 226. Retourner l'arbre binaire (bfs)
2022-06-26 17:15:00 【Toujours - - apprendre】
Description du sujet

Comment résoudre le problème
BFSEst au cœur de la résolution de ce problème,BFSLe noeud initial de est un noeud racine.
- Mettez d'abord le noeud racine dans le tableau.
- Extraire le premier élément du tableau,Et ensuite échanger les noeuds gauche et droit de cet élément.
- Si le noeud gauche existe,Ajoutez le noeud gauche au tableau.
- Si le noeud droit existe,Ajoutez le noeud droit au tableau.
ACCode
var invertTree = function(root) {
// UtiliserBFSRésoudre le problème de l'arbre binaire inversé
if (!root) return null;
const res = [root];
while (res.length) {
let cur = res.shift();
// Échanger les noeuds gauche et droit
[cur.left, cur.right] = [cur.right, cur.left];
if (cur.left) {
res.push(cur.left);
}
if (cur.right) {
res.push(cur.right);
}
}
return root;
};
Réflexion
Le miroir et l'inversion des arbres binaires sont un problème ,Dans l'ensemble, à traversBFS Peut résoudre ce problème efficacement .
边栏推荐
- Use FST JSON to automatically generate faster JSON serialization methods
- Necessary decorator mode for 3 years' work
- Interpretation of cloud native microservice technology trend
- Decentralized NFT transaction protocol will defeat opensea
- 14《MySQL 教程》INSERT 插入数据
- When I was in the library, I thought of the yuan sharing mode
- The high concurrency system is easy to play, and Alibaba's new 100 million level concurrent design quick notes are really fragrant
- Viewing the task arrangement ability of monorepo tool from turborepo
- What does the equals method compare? Who told you
- Getting started with mongodb
猜你喜欢

Platform management background and merchant menu resource management: Design of platform management background data service

Teach you to learn dapr - 4 Service invocation

Community ownership of NFT trading market is unstoppable

mysql Add column 失败 因为之前有数据,不是默认null 不行

ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要

SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用

Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)

Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)

Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!

Don't believe it, 98% of programmers are like this
随机推荐
Calculate the average of N numbers in the index group of X, and return the number that is less than the average and closest to the average through formal parameters
Viewing the task arrangement ability of monorepo tool from turborepo
Play with Linux and easily install and configure MySQL
Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
牛客网:设计LRU缓存结构 设计LFU缓存结构
Calculate the average of N numbers in the group indexed by the formal parameter x, move the data less than the average in the group indexed to the front of the array, and move the data greater than or
Teach you to learn dapr - 5 Status management
[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers
Deployment and operation of mongodb partitioned cluster
Teach you to learn dapr - 3 Run the first with dapr Net program
Necessary decorator mode for 3 years' work
Fgetc() reads content from file
mysql Add column 失败 因为之前有数据,不是默认null 不行
Jouer avec Linux et installer et configurer MySQL facilement
在国金证券开户怎么样?保障安全吗?
Microservice architecture practice: business management background and SSO design, SSO client design
SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用
Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation