当前位置:网站首页>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 .
边栏推荐
- Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
- Vue--vuerouter cache routing component
- 链游系统开发技术方案设计丨NFT链游系统开发流程及源码
- 类型多样的石膏PBR多通道贴图素材,速来收藏!
- Calculate the sum of the main diagonals of the array
- 对NFT市场前景的7个看法
- Count the number of words in a line of string and take it as the return value of the function
- SIGIR 2022 | University of Hong Kong and others proposed the application of hypergraph comparative learning in Recommendation System
- 数字藏品与NFT到底有何区别
- What is the preferential account opening policy of securities companies now? Is it safe to open an account online now?
猜你喜欢
【代码随想录-动态规划】T583、两个字符串的删除操作
vue--vuerouter缓存路由组件
Army chat -- registration of Registration Center
宝藏又小众的CTA动画素材素材网站分享
Web3去中心化存储生态图景
Turtle cartography
[C language] static modifies local variables
Platform management background and merchant menu resource management: merchant registration management design
When I was in the library, I thought of the yuan sharing mode
NFT 交易市场社区所有化势不可挡
随机推荐
Cache breakdown! Don't even know how to write code???
Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
Set up your own website (16)
物联网协议的王者:MQTT
Can Luo Yonghao succeed in entering the AR field this time?
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
Technical scheme design of chain game system development - NFT chain game system development process and source code
Redis overview
Kubecon China 2021 Alibaba cloud special session is coming! These first day highlights should not be missed
Programmer interview guide - self introduction
What does the equals method compare? Who told you
[recommendation system learning] recommendation system architecture
【推荐系统学习】推荐系统架构
num[i]++
7 views on NFT market prospect
A simple membership card management system based on Scala
Jouer avec Linux et installer et configurer MySQL facilement
Leetcode HOT100 (22--- bracket generation)
Summary of all knowledge points of C language
NFT 交易市场社区所有化势不可挡