Populating Next Right Pointers in Each Node (DFS,没想到)
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) {
if (root->left)
root->left->next = root->right;
if (root->right)
root->right->next = root->next ? root->next->left : NULL;
connect(root->left);
connect(root->right);
}
}
};
直接可以利用上一层的结果来DFS。
参考:http://blog.csdn.net/pickless/article/details/12027997
Populating Next Right Pointers in Each Node (DFS,没想到)的更多相关文章
- 117. Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- [LeetCode] 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] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 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 II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- 【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 ...
随机推荐
- 【HEVC帧间预测论文】P1.8 Complexity Control of High Efficiency Video Encoders for Power-Constrained Devices
参考:Complexity Control of High Efficiency Video Encoders for Power-Constrained Devices <HEVC标准介绍.H ...
- UVALive 2238 Fixed Partition Memory Management 固定分区内存管理(KM算法,变形)
题意:目前有一部分可用内存,分为m个大小固定的区域.现有n个程序要执行,每个程序在不同大小的内存中运行所需的时间不同,要使运行完所有程序所耗时最少,问每个程序在哪块区域中从什么时间运行到什么时间,以及 ...
- 简洁的KVO -- 使用Block响应事件
涉及内容: KVO,Runtime,Category,Block 首先创建NSObject的Category 举个例子是这样的: 随后定义你需要响应的Block结构 我简单一点就这样咯 typedef ...
- 关于JDBC访问存储过程的问题
最近开发一个应用,需要调用一个入参为List的存储过程. 存储过程为: proc_test(p1 OUT Number, p2 IN Number, p3 IN TAB_CUSTOMER); 这个Li ...
- docker部署xxl-job
资源 xxl-job:1.9.1 docker:17.05.0-ce maven:3.5.0-jdk-8 tomcat:8.5.23.0 mysql:5.6.40 一.创建数据库 克隆项目到服务器下 ...
- 448. Find All Numbers Disappeared in an Array@python
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
- Tomcat server.xml配置文件
server.xml配置文件: <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to ...
- crontab 和 supervisor
crontab linux系统自带的工具,可以做定时任务,最小间隔是1分钟 配置crontab 命令 如果是以root用户编辑的,那么最后运行也是以root用户运行脚本文件 crontab -e 命令 ...
- linux下solr5.0.0环境搭建
1解压 linux下解压命令 tar -zxvf solr-5.0.0.tgz 2启动 linux 系统 直接切换solr-5.0.0\bin 文件夹 执行 solr star -p 8983 (如果 ...
- [BZOJ 3823]定情信物
题面 定情信物 题解 这题主要考高中物理和数学. 首先定义 \(f[i][j]\) 表示 \(i\) 维超立方体中第 \(j\) 维元素的数量,根据实际意义,我们可以推出递推式: \(f[i][j]= ...