leetcode—Populating Next Right Pointers in Each Node
1.题目描述
Given a binary treestruct 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 7After calling your function, the tree should look like:1 -> NULL/ \2 -> 3 -> NULL/ \ / \4->5->6->7 -> NULL
2.解法分析
这道题目给了一个很强的约束,那就是可以假设树结果为满二叉树,满二叉树每一层的节点个数是确定的,如果将树中的元素按照层序遍历的方式编号,那么编号为n的节点在哪一层也是轻易可知的(假设编号从1开始,那么节点n所在层为log2n+1),同样,每层最后一个节点的编号也是已知的(m层的最后一个元素编号为2m-1),基于这个强约束,我决定用层序遍历的方式解答这个题目。
/*** 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) {// Start typing your C/C++ solution below// DO NOT write int main() functionif(root == NULL)return;queue<TreeLinkNode *> q;int count=0;int depth =1;TreeLinkNode * cur=NULL;q.push(root);while(!q.empty()){cur=q.front();q.pop();count++;if(count == pow(2,depth)-1){cur->next = NULL;depth++;}else{cur->next = q.front();}if(cur->left!=NULL)q.push(cur->left);if(cur->right!=NULL)q.push(cur->right);}}};
代码一次通过,真爽!
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 ...
随机推荐
- Python 3.5 for windows 10 通过pip安装mysqlclient模块 error:C1083
$pip install mysqlclient 运行结果如下: 可能是由于不兼容导致的(中间试过各种方法,比如本地安装mysql等等),最后找来mysqlclient-1.3.7-cp35-cp35 ...
- Windows系统下Oracle数据库冷备
一.背景: 具体的场景是数据库不是普通的OLTP系统,更像是OLAP系统,数据的更新频率很低,在noarchivelog 模式下运行,实时性要求低,但是数据只有一份不能弄丢,需要应付磁盘损坏等情况.这 ...
- android适应屏幕
生产android手机的厂商多不胜数,造就了android手机的屏幕尺寸也是不计其数.开发者为了使应用在各个品牌,各个型号的手机屏幕上保持一致的用户体验,就需要运用多种使应用的UI能适应不同屏幕尺寸的 ...
- Kinetic使用注意点--animation
new Animation(func, layers) 参数: func:每一帧都会调用一次此函数.此函数接收一个包含四个元素的参数对象,时间单位均为毫秒. { timeDiff:"上一帧和 ...
- Render Texture的使用(截取rendertexture的一帧到Texture2D)
游戏里人物角色太多,每个角色都要有张头像或全身照等,这样就必须截取大量的图片,花费大量的时间,有时截取的不满意还得重新截,即浪费时间又浪费精力.所以就想了个投机取巧的方法.那就是用unity搭建一个照 ...
- linux下定时发送邮件
at命令可以在某个时间运行某个程序,而mail可以以命令行的方式把存于一个文本中的邮件正文发送抄送出去. 具体用法: 1. 把email正文准备好,比如写在email.txt里 2. 然后写一个脚 ...
- Grails的redirect无法跳转时的一个可能原因
由于controller的命名一般首字母大写,如Login 此时如 class LoginController { def index = { redirect(action:Login, param ...
- HDU 2045 不容易系列之(3)—— LELE的RPG难题(递推)
点我看题目 题意 : 中文题不解释. 思路 :先算了第3个第4个,算的时候发现只要在已经枚举出来的前边的状态中往后添加字母就行了,如果两个的都已经表示出来了,那第三个就可以在每个第二个后边加一个,在 ...
- 练习--LINUX进程间通信之无名管道PIPE
IBM上放的这个系统不错,刚好可以系统回温一下LINUX的系统知识. http://www.ibm.com/developerworks/cn/linux/l-ipc/part1/ 感觉年纪大了,前几 ...
- easyui源码翻译1.32--SearchBox(搜索框)
前言 使用$.fn.searchbox.defaults重写默认值对象.下载该插件翻译源码 搜索框提示用户需要输入搜索的值.它可以结合一个菜单,允许用户选择不同的搜索类别.在用户按下回车键或点击组件右 ...
