当前位置:网站首页>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 .
边栏推荐
- MySQL index
- sparksql如何通过日期返回具体周几-dayofweek函数
- The high concurrency system is easy to play, and Alibaba's new 100 million level concurrent design quick notes are really fragrant
- [recommendation system learning] recommendation system architecture
- 一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
- The texstudio official website cannot be opened
- Programmer's essential toolkit, please collect!
- Use FST JSON to automatically generate faster JSON serialization methods
- vue--vuerouter缓存路由组件
- Detailed contract quantification system development scheme and technical description of quantitative contract system development
猜你喜欢
![[suggested collection] 11 online communities suitable for programmers](/img/6b/d5c68e93384fd314d0cb27d9df1cb9.jpg)
[suggested collection] 11 online communities suitable for programmers

Leetcode HOT100 (22--- bracket generation)

ACL 2022 | zero sample multilingual extracted text summarization based on neural label search

并发之Synchronized说明

分布式缓存/缓存集群简介
![[recommendation system learning] recommendation system architecture](/img/a8/448f6e708227555bb6b32cdc652435.png)
[recommendation system learning] recommendation system architecture

玩轉Linux,輕松安裝配置MySQL

Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)

Introduction to distributed cache / cache cluster

并发之线程安全
随机推荐
Romance of the Three Kingdoms: responsibility chain model
7 views on NFT market prospect
Synchronized description of concurrency
并发之Synchronized说明
Demonstrate to Xiaobai the case of sub database and sub table
Troubleshooting ideas that can solve 80% of faults!
Convert the decimal positive integer m into the number in the forward K (2 < =k < =9) system and output it in bits
量化合约系统开发分析案例丨合约量化系统开发方案详解
[latex bearer] use tables in \title (error \begin doesn't match its definition.)
Leetcode topic [array] -283- move zero
Wechat app mall, review products, upload commodity pictures, and score Commodity Services
Army chat -- registration of Registration Center
10 cloud security best practices that enterprises need to know
链游系统开发技术方案设计丨NFT链游系统开发流程及源码
Teach you to learn dapr - 6 Publish subscription
Teach you to learn dapr - 5 Status management
【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
在国金证券开户怎么样?保障安全吗?
Cloud native 02: Alibaba cloud cloud efficient flow pipeline
Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation