当前位置:网站首页>LeetCode——226. Flip binary tree (BFS)
LeetCode——226. Flip binary tree (BFS)
2022-06-26 17:15:00 【Always--Learning】
Title Description

Their thinking
BFS Is the core to solve this problem ,BFS The initial node of is a root node .
- First, put the root node into the array .
- Get the first element in the array , Then swap the left and right nodes of this element .
- If the left node exists , Add the left node to the array .
- If the right node exists , Then add the right node to the array .
AC Code
var invertTree = function(root) {
// Use BFS Solve the problem of flipping binary tree
if (!root) return null;
const res = [root];
while (res.length) {
let cur = res.shift();
// Switch left and right nodes
[cur.left, cur.right] = [cur.right, cur.left];
if (cur.left) {
res.push(cur.left);
}
if (cur.right) {
res.push(cur.right);
}
}
return root;
};
reflection
Binary tree mirroring and reversing binary tree is a topic , On the whole, through BFS It can solve this problem efficiently .
边栏推荐
- VSCode使用 - Remote-SSH 配置说明
- Play with Linux and easily install and configure MySQL
- 玩轉Linux,輕松安裝配置MySQL
- The high concurrency system is easy to play, and Alibaba's new 100 million level concurrent design quick notes are really fragrant
- Teach you to learn dapr - 2 Must know concept
- MySQL index
- 一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
- Necessary decorator mode for 3 years' work
- The function keeps the value of variable H to two decimal places and rounds the third digit
- Treasure and niche CTA animation material website sharing
猜你喜欢

Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack

去中心化NFT交易协议将击败OpenSea
![[matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion](/img/a6/6d3914360ffe4732db0dbd2aaf1994.png)
[matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion

Discussion: the next generation of stable coins

牛客网:设计LRU缓存结构 设计LFU缓存结构

Environment setup mongodb

Web3去中心化存储生态图景

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

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

Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
随机推荐
Teach you to learn dapr - 3 Run the first with dapr Net program
Cache breakdown! Don't even know how to write code???
物联网协议的王者:MQTT
num[i]++
Teach you to learn dapr - 2 Must know concept
[Error] ld returned 1 exit status
COMP5216 Mobile Computing Assignment 1 - Extending ToDoList app
vue--vuerouter缓存路由组件
halcon之区域:多种区域(Region)特征(5)
Discover K8E: minimalist kubernetes distribution
Daily record 2
Byte interview: two array interview questions, please accept
Technical scheme design of chain game system development - NFT chain game system development process and source code
Redis' 43 serial cannons, try how many you can carry
C language -- legal identifier and integer
Some explanations for latex CJK
[C language] static modifies local variables
并发之线程安全
[suggested collection] 11 online communities suitable for programmers
Redis and database data consistency