【leetcode】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node 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
只是当我们没有找到时,father=father->next;
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) { if(root==NULL)
{
return;
} if(root->left!=NULL)
{
if(root->right!=NULL)
{
root->left->next=root->right;
}
else
{
TreeLinkNode *tmp=root->next;
root->left->next=NULL;
while(tmp!=NULL)
{
if(tmp->left!=NULL)
{
root->left->next=tmp->left;
break;
} if(tmp->right!=NULL)
{
root->left->next=tmp->right;
break;
}
tmp=tmp->next;
}
}
} if(root->right!=NULL)
{
TreeLinkNode *tmp=root->next;
root->right->next=NULL;
while(tmp!=NULL)
{
if(tmp->left!=NULL)
{
root->right->next=tmp->left;
break;
} if(tmp->right!=NULL)
{
root->right->next=tmp->right;
break;
}
tmp=tmp->next;
}
} connect(root->right);
connect(root->left); }
};
【leetcode】Populating Next Right Pointers in Each Node II的更多相关文章
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
- 【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 ...
- 【题解】【BT】【Leetcode】Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- [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 II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...
- 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 ...
随机推荐
- HTML 简介
什么是HTML HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言: Hyper Text Markup Language HTML 不是一种编程语言,而是一种标记语言 标记语言是一 ...
- 全选,全不选,反选的js实现
全选练习 ** 使用复选框上面一个属性判断是否选中 - checked属性 - checked=true:选中 ...
- jqxTreeGrid
基本TreeGrid样本 <!DOCTYPE html> <html lang="en"> <head> <title id=" ...
- asp.net webform中使用async,await实现异步操作
摘要 最近想着将项目中的部分耗时的操作,进行异步化.就自己弄个demo进行学习.只需下面几个步骤就可以将aspx页面中注册异步操作. demo 比如我们需要抓取某个url的内容,这个时候我们可能会有下 ...
- 【9-20】vimtutor学习笔记
第一节 ghjk移动光标 :q!:强制退出vim x:删除光标处的字符 i:在光标处插入 A:附加文本 :wq:保存文档并退出 第二节 dw:删除一个单词 d$:删除至行尾 de:删除光标处到该单词结 ...
- 今天依然是 JQ点击事件之“点击淡入淡出事件”
废话不多说,先贴代码,再解释 <script> $(document).ready(function(){ $("button").click(function(){ ...
- CString::Mid成员函数
CString Mid( int nFirst, int nCount ) const; 此成员函数从此CString对象中提取一个长度为nCount个字符的子串,从nFirst(从零开始的索引)指定 ...
- linux下的库冲突问题
lib1.c #include <stdio.h>int fun(){ printf("lib1\n"); return 0;} lib2.c #include < ...
- taglib
thinkphp中 taglib标签应用 原文出处:http://blog.csdn.net/a11085013/article/details/38172653 1.配置文件中加上: 'APP_AU ...
- 2015Summer Training #2
暑假集训昨天刚开始,14级的小伙伴快到齐了,hhhhh ,毕竟下学期区域赛,对我们来说还是很困难的. 打算每天写份总结. UVA 11300 C.Spreading the Wealth 题目大意:n ...