【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
/**
* 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) root->left->next=root->right;
if(root->right!=NULL)
{
if(root->next!=NULL) root->right->next=root->next->left;
}
connect(root->left);
connect(root->right); }
};
【leetcode】Populating Next Right Pointers in Each Node的更多相关文章
- 【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 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 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】【Medium】Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 【树】Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
- 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 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... ...
随机推荐
- 在VS2013中强制IIS Express应用程序池使用经典模式
直接在文件夹地址栏输入 %userprofile%\documents\iisexpress\config\applicationhost.config 会打开上边的配置文件 将 <siteDe ...
- php 数组排序以及按照某个字段排序
经常,开发人员发现在PHP中使用这种数据结构对值或者数组元素进行排序非常有用.PHP提供了一些适合多种数组的排序函数,这些函数允许你在数组内部对元素进行排列,也允许用很多不同的方法对它们进行重新排序. ...
- webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成
首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...
- 今天是JQ 的slideUp 和 slideDown 的点击事件
先贴代码,再讲详细事件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...
- web网页颜色色谱
snow 255 250 250 #fffafa ghostwhite 248 248 255 #f8f8ff whitesmoke 245 245 245 #f5f5f5 gainsboro 220 ...
- GCD与NSOperationQueue
1> GCD是纯C语言的API,NSOperationQueue是基于GCD的OC版本封装 2> GCD只支持FIFO(先入先出)的队列,NSOperationQueue可以很方便地调整执 ...
- activiti数据库表结构全貌解析
http://www.jianshu.com/p/e6971e8a8dad 下面本人介绍一些activiti这款开源流程设计引擎的数据库表结构,首先阐述:我们刚开始接触或者使用一个新的东西(技术)时我 ...
- Bots(逆元,递推)
H. Bots time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input out ...
- java自定义标签 权限
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java ...
- vim编译运行java程序
想让你的vim可以编译运行java只要在你的配置文件里面加一句: map <F5> :!javac %&&java %:r <CR> 就可以编译运行java了