【题解】【BT】【Leetcode】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
思路:
这题看似Binary Tree Level Order Traversal,实则因为满二叉树的设定,相比要简单太多,提前计算好每层的节点数就OK
代码:
void connect(TreeLinkNode *root) {
if(root == NULL) return;//考虑自身为空
queue<TreeLinkNode*> nodeQ;
nodeQ.push(root);
int n = ;
int i = ;
while(!nodeQ.empty()){
TreeLinkNode* top = nodeQ.front();
nodeQ.pop();
if(++i != n){
top->next = nodeQ.front();
}else{
n *= ;
i = ;
}
if(top->left != NULL)//考虑左右子树为空(叶子节点)的情况
nodeQ.push(top->left);
if(top->right != NULL)
nodeQ.push(top->right);
}
}
【题解】【BT】【Leetcode】Populating Next Right Pointers in Each Node的更多相关文章
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- [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 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 @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- 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 解题报告
Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode { Tree ...
- [LeetCode] [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 ...
随机推荐
- Graphical installers are not supported by the vm
http://www-01.ibm.com/support/docview.wss?uid=swg21462180 Technote (troubleshooting) Problem(Abstrac ...
- tomcat项目发布 更改小猫图标 及自定义错误404界面
tomcat发布项目的时候遇到些小问题 不过解决了 问题1. 整个服务器的404自定义界面问题 解决方法: 在tomcat安装目录下conf中web.xml中修改配置文件 <error-page ...
- 两天以来对plsqldev操作的记忆
frist,开机后,打开服务,打开服务,打开服务(重要的事情说三遍). and then, 打开plsqldev,输入TEST账户而不是SYS账户,否则你会被许多未接触到的内容淹没你刚刚创建好的表. ...
- 二模 (9) day2
第一题: 题目大意:求满足条件P的N位二进制数的个数.P:该二进制数有至少3个0或者3个1挨在一起.. N<=20000 解题过程: 1.一开始直接写了个dfs把表打了出来,不过没发现什么规律, ...
- C#获取本机mac地址
添加System.Management的引用, using System.Management; string mac = ""; ManagementClass mc = new ...
- MySql插入记录时判断
我们在开发数据库相关的逻辑过程中, 经常检查表中是否已经存在这样的一条记录, 如果存在则更新或者不做操作, 如果没有存在记录,则需要插入一条新的记录. 这样的逻辑固然可以通过两条sql语句完成. SE ...
- arm-linux-gcc-4.3.2安装步骤
安装交叉编译工具链: 1.首先以root用户登入 2.复制arm-linux-gcc-4.3.2.tgz到根目录下tmp文件夹里 3.解压命令tar xvzf arm-linux-gcc-4. ...
- javascript photo http://www.cnblogs.com/5ishare/tag/javascript/
- JavaScript原生对象属性和方法详解——Array对象
http://www.feeldesignstudio.com/2013/09/native-javascript-object-properties-and-methods-array/ lengt ...
- jsoup Cookbook(中文版)--爬虫(java)
转载:http://www.open-open.com/jsoup/ 目录: 入门 解析和遍历一个html文档 输入 解析一个html字符串 解析一个body片断 根据一个url加载Document对 ...