当前位置:网站首页>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 .
边栏推荐
- Play with Linux and easily install and configure MySQL
- Classical synchronization problem
- Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack
- Redis OM . Net redis object mapping framework
- Redis overview
- 20:第三章:开发通行证服务:3:在程序中,打通redis服务器;(仅仅是打通redis服务器,不涉及具体的业务开发)
- Leetcode topic [array] -283- move zero
- 宝藏又小众的CTA动画素材素材网站分享
- She said she was tired! So tired! I want to change my career
- 20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
猜你喜欢

探讨:下一代稳定币

When I was in the library, I thought of the yuan sharing mode

Basic requirements: 7 problems in singleton mode

sparksql如何通过日期返回具体周几-dayofweek函数

Teach you to learn dapr - 2 Must know concept

Introduction to minimal API

Summary of all knowledge points of C language

Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class

Fire evacuation and self rescue... This safety production and fire training is full!

20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
随机推荐
【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
Byte interview: two array interview questions, please accept
10 cloud security best practices that enterprises need to know
Programmer interview guide - self introduction
Kubecon China 2021 Alibaba cloud special session is coming! These first day highlights should not be missed
NFT 交易市场社区所有化势不可挡
7 views on NFT market prospect
Programmer's essential toolkit, please collect!
玩转Linux,轻松安装配置MySQL
Deployment and operation of mongodb partitioned cluster
Army chat -- registration of Registration Center
What does the equals method compare? Who told you
直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!
The function keeps the value of variable H to two decimal places and rounds the third digit
Sandboxed container: container or virtual machine
Distributed Architecture Overview
Community ownership of NFT trading market is unstoppable
No manual prior is required! HKU & Tongji & lunarai & Kuangshi proposed self supervised visual representation learning based on semantic grouping, which significantly improved the tasks of target dete
Vue--vuerouter cache routing component
Necessary decorator mode for 3 years' work