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 ...
随机推荐
- 小程序发送 request请求失败 提示不在合法域名列表中的解决方法
可以在小程序开发工具中设置不校验域名.
- OFMessageDecoder 分析
OFMessageDecoder 继承了抽象类 FrameDecoder.FrameDecoder 会将接收到的ChannelBuffers 转换成有意义的 frame 对象.在基于流的传输 ...
- springboot 2.0 配置 logback
springboot2.0默认已经引入日志jar依赖,所以直接配置日志信息就可以了. 在application.properties中加入: logging.config=classpath:logb ...
- activiti 发布异常 org.activiti.engine.ActivitiException: Error parsing XML
三月 23, 2015 1:58:31 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() fo ...
- Ajax跨域请求action方法,无法传递及接收cookie信息(应用于系统登录认证及退出)解决方案
最近的项目中涉及到了应用ajax请求后台系统登录,身份认证失败,经过不断的调试终于找到解决方案. 应用场景: 项目测试环境:前端应用HTML,js,jQuery ajax请求,部署在Apache服务器 ...
- Python编程-异常处理
一.错误和异常 1.程序中难免出现错误,而错误分成两种 (1)语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 def t ...
- C++中substr的用法
C++中substr函数的用法 #include<string> #include<iostream> using namespace std; void main() { s ...
- H3C光模块相关命令和检测方法
<Sysname> dis transceiver interface GigabitEthernet 1/0/28 查看 GigabitEthernet1/0/28 transcei ...
- python机器学习——分词
使用jieba库进行分词 安装jieba就不说了,自行百度! import jieba 将标题分词,并转为list seg_list = list(jieba.cut(result.get(" ...
- [kuangbin带你飞]专题十 匹配问题 一般图匹配
过去做的都是二分图匹配 即 同一个集合里的点 互相不联通 但是如果延伸到一般图上去 求一个一般图的最大匹配 就要用带花树来解决 带花树模板 用来处理一个无向图上的最大匹配 看了一会还是不懂 抄了一遍 ...