[Leetcode Week15]Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/
Description
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).
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
Solution
class Solution {
private:
void connectNode(vector<TreeLinkNode*>& v) {
int size = v.size();
for (int i = 0; i <= size - 2; i++) {
v[i] -> next = v[i + 1];
}
}
public:
void connect(TreeLinkNode *root) {
if (root == NULL)
return;
int levelNodeNum = 1;
int curLevelNodeNum = 0;
queue<TreeLinkNode*> q;
vector<TreeLinkNode*> v;
q.push(root);
while (!q.empty()) {
TreeLinkNode* node = q.front();
q.pop();
v.push_back(node);
if (node -> left != NULL)
q.push(node -> left);
if (node -> right != NULL)
q.push(node -> right);
curLevelNodeNum++;
if (curLevelNodeNum == levelNodeNum) {
levelNodeNum *= 2;
curLevelNodeNum = 0;
connectNode(v);
v.clear();
}
}
}
};
解题描述
这道题是关于二叉树层次遍历问题的变种。题目给出的二叉树是完全二叉树,所以可以提前算出每一层的节点数目,因此来说还是相对比较容易的。所以基本的解决办法是,使用一个队列来存放节点。最开始将根节点加入队列。每次从队首取出一个节点,将其子节点加入队尾。然后使用一个计数变量来计算当前层次上已经加入队列的节点数目。一旦达到该层次的节点数目总和就对该层的节点进行next连接。
[Leetcode Week15]Populating Next Right Pointers in Each Node的更多相关文章
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- 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 ...
- 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 ...
随机推荐
- Matlab中save与load函数的使用
用save函数,可以将工作空间的变量保存成txt文件或mat文件等. 比如: save peng.mat p j 就是将工作空间中的p和j变量保存在peng.mat中. 用load函数,可以将数据读入 ...
- nopi导出
1.NPOI官方网站:http://npoi.codeplex.com/ 可以到此网站上去下载最新的NPOI组件版本 2.NPOI在线学习教程(中文版): http://www.cnblogs.com ...
- 响应式布局(rem布局),使用JS动态设置fontsize
var cw = document.documentElement.clientWidth; if (cw > 1920) { cw = 1920; } document.getElements ...
- openssl安装相关软件
出现:error: openssl/md5.h: No such file or directory 原因是libssl-dev 没有安装,执行: sudo apt-get install libss ...
- JavaScript是没有域的限制
baidu的通行证处理都是在二级域名passport.baidu.com中处理的,但是baidu很多地方登录都好像是用ajax处理的,他是怎么做的呢?研究了一下,发现一个小技巧. 在http://zh ...
- Python通过PhantomJS获取JS渲染后的网页源代码
新建一个文件,命名为test.js,内容如下: var page = require('webpage').create(), system = require('system'), address; ...
- 牛客328B Rabbit的工作(1)
传送门:https://ac.nowcoder.com/acm/contest/328/B 题意:给你n和k,和一个长度为n的字符串,字符串中的0表示休息,1表示工作,连续工作会损耗连续的体力值,从1 ...
- idea一些文件如.xml 文件搜索不到的解决方法
- bug report
ubuntu 11.10添加eth0:1后重启网卡不显示 eth0:1 http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=324306
- Hive分组后取组内排名方法row_number
今天遇到这样一个需求场景,要取出 每一种分类(a,b组合分类) 符合条件的日期(字段c) 距离现在最近的10个日期 的数据 首先想到的是用sql筛选出符合某种条件的所有数据,这样的事情很简单 然后用脚 ...