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 解法一:
先不考虑空间复杂度限制 从图例就可以得到解法,就是层次遍历。
每一层的前一个节点next指向后一个节点,层次之间不相连。
也就是说,
如果当前加入的节点cur是左子结点,那么需要判断一下,该节点是不是新一层的第一个节点。
若是:上一个节点pre的next不需要指向当前节点
若否:上一个节点pre的next需要指向当前节点
如果当前加入的节点cur是右子结点,那么不需要判断,上一个节点pre的next需要指向当前节点。
/**
* 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:
queue<TreeLinkNode*> q;
void connect(TreeLinkNode *root)
{
if(root == NULL)
return; int ind_count = ;
int pow_count = ; q.push(root);
ind_count ++;
TreeLinkNode *cur = root;
TreeLinkNode *pre = root;
TreeLinkNode *temp = root; while(!q.empty())
{
temp = q.front();
q.pop(); if(temp->left)
{
q.push(temp->left);
pre = cur;
cur = temp->left;
ind_count ++;
if(ind_count != pow(2.0, pow_count))
pre->next = cur;
else
pow_count ++;
}
if(temp->right)
{
q.push(temp->right);
pre = cur;
cur = temp->right;
ind_count ++;
pre->next = cur;
}
}
}
};

解法二:
接下来我们考虑怎样去掉队列进行层次遍历。
首先我们需要一个指示器,告诉我们每一层的开始,然后在遍历该层的时候将下一层的next进行连接。(遍历依赖于上一层建立好的next)
/**
* 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:
queue<TreeLinkNode*> q;
void connect(TreeLinkNode *root)
{
if(root == NULL)
return; TreeLinkNode* leftWall = root;
while(leftWall != NULL)
{// for each level
TreeLinkNode* cur = leftWall;
while(cur != NULL)
{// visit each node of this level
if(cur->left)
cur->left->next = cur->right;
if(cur->right && cur->next)
cur->right->next = cur->next->left;
cur = cur->next;
}
leftWall = leftWall->left;
}
}
};

【LeetCode】115. Populating Next Right Pointers in Each Node (2 solutions)的更多相关文章

  1. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  2. 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  3. 【LeetCode】116. Populating Next Right Pointers in Each Node

    题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...

  4. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  5. 【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  6. 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 ...

  7. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  8. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

  9. LeetCode OJ: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 ...

随机推荐

  1. 【BZOJ】【2435】【NOI2011】道路修建

    DFS/DP 本来以为是一道傻逼题,然而跪了好久……一直RE…… 直接dfs就好了……x->y val=c  :  ans+=abs(n-size[y]-size[y])*c; 然而为啥会一直R ...

  2. Cesium随笔(2)加载天地图地图服务【转】

    Cesium自带的图层是bing地图和esri,mapbox等图层,木有中文注记,想加载中文注记的图层?废话不说,上代码: var viewer = new Cesium.Viewer('cesium ...

  3. Andorid之ActivityManager

    在Android中ActivityManager主要用于和系统中运行的Activities进行交互.在本篇文章中,我们将对ActivityManager中的API进行研究使用. 在ActivityMa ...

  4. 转: Nginx proxy讲解精华文章集

    1. 详细,参数说明很好 https://blog.lyz810.com/article/2016/06/ngx_stream_proxy_module_doc_zh-cn/

  5. Retrofit2+Rxjava+MVP实践

    此博文根据前面两篇文章 Android MVP 架构初试 Android MVP 架构封装 再结合主流框架Retrofit2+Rxjava来个实践 源码地址RxMVP 项目截图 Retrofit2+R ...

  6. C语言:strcpy()和memcpy()

    一.strcpy和memcpy都是标准C库函数,它们有下面的特点:      1.strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符. ...

  7. linux下线程调试 ulimit core

    在linux 下写线程程序的同学预计都遇到过找bug找到崩溃的情况.多线程情况下bug的追踪实在是不easy. 如今我来介绍一个好用的方法 ulimit core. 先简介一下ulimit是个什么(你 ...

  8. ZH奶酪:putty远程登录Linux服务器非常慢

    11.pytty远程登录Linux服务器非常慢 http://www.it165.net/os/html/201209/3425.html 12.启动SSHD服务报错 http://blog.chin ...

  9. leetcode Wildcard Matching greedy algrithm

    The recursive program will result in TLE like this: class Solution { public: bool isMatch(const char ...

  10. Java从零开始学三十九(对象序列化)

    一.序列化 将对象的状态存储到特定存储介质中的过程 对象序列化,就是把一个对象变为二进制的数据流的一种方法,通过对象序列化可以方便的实现对象的传输或存储.   序列化保存对象的“全景图”,构建对象的“ ...