LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1
/ \
2 3
/ \ \
4 5 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
指出每个节点的下一个右侧节点,前一篇博客的那个思路也可以继续使用,但由于这里的二叉树是任意的,所以应该用函数找到下一层的开始节点以及下一层的下一个节点,代码如下:
class Solution{
public:
void connect(TreeLinkNode *root)
{
TreeLinkNode * prev, * curr, * start;
if(!root) return;
while(root){
start = findNextLevelStartNode(root);
prev = start;
curr = findNextLevelNextNode(root, prev);
while(curr){
prev->next = curr;
prev = curr;
curr = findNextLevelNextNode(root, curr);
}
root = start;
}
}
private:
TreeLinkNode * findNextLevelNextNode(TreeLinkNode * & node, TreeLinkNode * curr)//注意使用引用
{
if(node->left == curr && node->right)
return node->right;
else{
while(node->next){
node = node->next;
if(node->left != NULL && node->left != curr) return node->left;
if(node->right != NULL && node->right != curr) return node->right;
}
}
return NULL;
}
TreeLinkNode * findNextLevelStartNode(TreeLinkNode * node)
{
if(!node) return NULL;
if(node->left)
return node->left;
else return findNextLevelNextNode(node, node->left);
}
};
LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)的更多相关文章
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针
You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...
- 【leetcode】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
随机推荐
- activiti部署到linux后流程图不显示汉字的问题
linux和windows的字体文件一般是不一样的,默认情况下,linux的java7中一般不支持中文,activiti的动态流程图中的汉字需要java调用汉字的字库,这里需要配置一下java的汉字字 ...
- Java - 在控制台中执行一个可执行jar
1.Maven打包一个可执行jar: <build> <plugins> <plugin> <groupId>org.apache.maven.plug ...
- $MySQL常用操作命令备忘
1.登录数据库:mysql -uroot -p12345 (12345为密码)2.创建数据库:create database senguocc; (senguocc为数据库名)3.查看有哪些数据库: ...
- JAVA中的Token 基于Token的身份验证
最近在做项目开始,涉及到服务器与安卓之间的接口开发,在此开发过程中发现了安卓与一般浏览器不同,安卓在每次发送请求的时候并不会带上上一次请求的SessionId,导致服务器每次接收安卓发送的请求访问时都 ...
- redis操作封装整理
<?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...
- 【HackerRank】The Love-Letter Mystery
James找到了他的朋友Harry要给女朋友的情书.James很爱恶作剧,所以他决定要胡搞一下.他把信中的每个单字都变成了回文.对任何给定的字符串,他可以减少其中任何一个字符的值,例如'd'可以变成' ...
- Python编程-架构、Socket
一.客户端/服务器架构 1.C/S架构 Client/Server架构,即服务器/客户端架构. 客户端和服务器端的程序不同,用户的程序主要在客户端,服务器端主要提供数据管理.数据共享.数据及系统维护和 ...
- Hyperledger Fabric1.0环境搭建
一.准备CentOS系统,本文使用的是CentOS7.0 二.安装Docker 执行命令 yum -y install docker 验证是否安装成功 docker --version 三.安装Doc ...
- openpyxl之excel操作
一.读取excel中内容 1.导入模块 : from openpyxl import load_workbook 2.打开excel : workbook = load_workbook(" ...
- 20145210姚思羽《网络对抗》Web基础
20145210姚思羽<网络对抗>Web基础 实验后回答问题 (1)什么是表单 表但是与用户交互的窗口,负责采集网页中的数据,允许用户在表单中输入信息. (2)浏览器可以解析运行什么语言. ...