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 ...
随机推荐
- 如何在JS中获取Request方法
方法 function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = ne ...
- redhat 5.4 下rabbitMQ单机安装.md
1. 系统版本 `cat /etc/redhat-release` `Red Hat Enterprise Linux Server release 5.4 (Tikanga)` 2. 下载软件包 ...
- mongodb Install the MongoDB service
在用到mongodb时,首先要运行mongod.exe以启动mongo,这样就会出现命令框( command prompt),为了避免出现这种情况.要以服务的形式来启动mongo,这样就不会出现命令框 ...
- python与编码
Python中的文字对象 Python 3.x中处理文字的对象有str, bytes, bytearray. bytes和bytearray可以使用除了用作格式化的方法(format, format_ ...
- Bootstrap 与 ASP.NET MVC 4 不使用 NuGet Package 笔记
转自 http://www.mytecbits.com/microsoft/dot-net/bootstrap-with-asp-net-mvc-4-step-by-step 单位最近做了一个Boot ...
- PHP与最丑的后台管理系统
第二天阿Q到公司还是比较早,同事只有阿梅在,阿Q坐在椅子上旋转来旋转去,有点像个小孩子.公司有书柜,书柜上放了好几本很新的php的书,.net的书反倒比较少而且显得老旧.阿Q起身走过去拿了本php翻了 ...
- hdu 3480
斜率dp #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...
- Oracle----Key Word
desc|describe table_name DCL----column ----add -- add one column alter table product ); -- add multi ...
- JavaScript注入漏洞的原理及防范
初次接触: 初次接触JavaScript注入漏洞后,如果不对这种漏洞的作用机理仔细分析并提取出其发生的某种模式,你就不能做到快速的发现项目中可能存在的所有注入风险并在代码中防范. 发生模式: Java ...
- SPRING IN ACTION 第4版笔记-第四章Aspect-oriented Spring-001-什么是AOP
一. Aspect就是把会在应用中的不同地方重复出现的非业务功能的模块化,比如日志.事务.安全.缓存 In software development, functions that span mult ...
