惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
NISL@THU
NISL@THU
T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
Cyberwarzone
Cyberwarzone
D
Docker
The Hacker News
The Hacker News
C
CERT Recently Published Vulnerability Notes
Vercel News
Vercel News
Project Zero
Project Zero
S
Schneier on Security
aimingoo的专栏
aimingoo的专栏
I
Intezer
腾讯CDC
M
MIT News - Artificial intelligence
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Vulnerabilities – Threatpost
G
Google Developers Blog
N
Netflix TechBlog - Medium
The Cloudflare Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
A
Arctic Wolf
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
Recent Announcements
Recent Announcements
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 热门话题
T
Threatpost
Latest news
Latest news
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
The GitHub Blog
The GitHub Blog
T
Tor Project blog
P
Proofpoint News Feed

博客园 - 谢绝围观

LeetCode 910. Smallest Range II EAC 抓取CD为AAC文件 Binary Indexed Tree (Fenwick Tree) Lint Code 1365. Minimum Cycle Section LintCode 896. Prime Product 简明题解 UVa 1471 - Defense Lines Windows下安装配置MinGW GCC调试环境 详解LeetCode 137. Single Number II LeetCode 309. Best Time to Buy and Sell Stock with Cooldown LeetCode 84. Largest Rectangle in Histogram 修改注册表删除Windows资源管理器 “通过QQ发送” 右键菜单项 LeetCode 312. Burst Balloons LeetCode 287. Find the Duplicate Number LeetCode 10. Regular Expression Matching 在Windows Azure VM下搭建SSTP VPN - 谢绝围观 利用.NET Code Contracts实现运行时验证 Log4net 配置实例 解决Windows Server 2012 下利用RRAS创建VPN断线的问题 - 谢绝围观 慎用静态类static class
LeetCode 117. Populating Next Right Pointers in Each Node II
谢绝围观 · 2016-02-21 · via 博客园 - 谢绝围观

原题地址

大意是,给定二叉树的每个节点都有一个next指针,希望能遍历一遍二叉树使每个节点的next指针指向其同一级的右侧节点(最右侧节点的next指针始终置为空)

如:

         1
       /  \
      2    3
     / \    \
    4   5    7

变换后:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

基本思路是按层序遍历(BFS)来设置指针。因为有next指针的存在,遍历每层时,该层的next指针已经在上一层设置好,所以只需按照next指向的顺序遍历该层即可。只需常量空间。代码及解释如下:

 1 using namespace std;
 2 
 3 struct TreeLinkNode {
 4 int val;
 5 TreeLinkNode *left, *right, *next;
 6 TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 7 };
 8 
 9 class Solution {
10 public:
11     void connect(TreeLinkNode *root) {
12         TreeLinkNode *parentLevelPtr = root;
13         TreeLinkNode *pseudoNode = new TreeLinkNode(0);
14         TreeLinkNode *childLevelHead, *childLevelPtr;
15         // 分别为父子层节点准备两套指针,父节点层指针parentLevelPtr按父节点层的next指针往后走。
16         for(; parentLevelPtr != NULL; pseudoNode->next = NULL){ // 每次循环结束需要重置pseudoNode的next指针,否则遍历到叶子节点层时循环无法结束
17             // 子节点层两个指针childLevelHead和childLevelPtr,初始置为指向一个虚拟节点。使用虚拟节点的好处是不用在循环体内判断是否需要给head赋值。
18             childLevelPtr = childLevelHead = pseudoNode;
19             for(; parentLevelPtr; parentLevelPtr = parentLevelPtr->next){
20                 // 父节点指针每移动一次,判断父节点是否有左右儿子,若有则将childLevelPtr的next置为此儿子节点,并将childLevelPtr移至next。
21                 if(parentLevelPtr->left){
22                     childLevelPtr->next = parentLevelPtr->left;
23                     childLevelPtr = childLevelPtr->next;
24                 }
25                 
26                 if(parentLevelPtr->right){
27                     childLevelPtr->next = parentLevelPtr->right;
28                     childLevelPtr = childLevelPtr->next;
29                 }
30             }
31             // 父层遍历完毕,将父层节点指向儿子层继续下一轮
32             parentLevelPtr = childLevelHead->next;
33         }
34         delete pseudoNode;
35     }
36 };

示意图