当前位置:网站首页>[JS solution] leedcode 117 Populate the next right node pointer II for each node
[JS solution] leedcode 117 Populate the next right node pointer II for each node
2022-06-13 04:40:00 【xiaozhangtxue】
Subject requirements :
Given a binary tree
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
Fill in each of its next The pointer , Let this pointer point to its next right node . If the next right node cannot be found , Will next The pointer is set to NULL.
In the initial state , all next The pointer is set to NULL.
Example :
Input :root = [1,2,3,4,5,null,7]
Output :[1,#,2,3,#,4,5,7,#]
explain : A given binary tree is shown in figure A Shown , Your function should fill in every one of its next The pointer , To point to its next right node , Pictured B Shown . The serialized output is in sequence traversal order ( from next Pointer connection ),‘#’ Represents the end of each layer .
JS Code
var connect = function(root) {
// Deal with special situations
if (!root) return null
// Building hierarchically traversed arrays
let arr = [root]
while(arr.length){
let n = arr.length
for(let i=0;i<n;i++){
// Throw the current node
let curNode = arr.shift()
// If it is the last node, it points to null
if(i===n-1) curNode.next=null
// If it's not the last node , Then point to the next node ( That is to say arr The first node in )
else curNode.next= arr[0]
// Continue to traverse the left and right children
if(curNode.left!==null) arr.push(curNode.left)
if(curNode.right!==null) arr.push(curNode.right)
}
}
return root
};
Running results
边栏推荐
- Consolidated figures
- The processing flow of thread pool depends on the core parameters
- Analyse du principe de mise en œuvre d'un éditeur de texte open source markdown - to - rich
- promise处理js多线程全部获得结果后同一处理结果
- Use go to add massive data to MySQL
- Powershell 加域 Add-Computer模块
- Third party comment plugin
- Internet people a few years ago vs Internet people today
- Clear timer failure
- Redis master-slave replication, sentinel mode, cluster
猜你喜欢
Express scaffold creation
Introduction to applet Basics (dark horse learning notes)
[try to hack] upload labs (temporarily write to 12)
如何只用4步,实现一个自定义JDBC驱动?
Crawler scrapy framework learning 1
Catalan number
Cesium:CesiumLab制作影像切片与切片加载
2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
Express framework knowledge - Art template template, cookie, session
随机推荐
It's the Caesar code. (*‘▽‘*)*
Catalan number
PowerShell plus domain add computer module
Implementation of article list function on PHP 18 home page
General communication protocol for industrial Internet
力扣刷题338.比特位计数
2022年氧化工艺操作证考试题库及模拟考试
利用Javeswingjdbc基於mvc設計系統
Applet version update
Express scaffold creation
How to implement a custom jdbc driver in only four steps?
Redis主从复制、哨兵模式、集群
The differences between the four startup modes of activity and the applicable scenarios and the setting methods of the two startup modes
Third party comment plugin
Application of dagger2 learning module (II)
Time format method on the official demo of uniapp
Suffix Automaton
How to handle async/await error messages gracefully
Internet people a few years ago vs Internet people today
Message scrolling JS implementation