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. mysql的TABLE_SCHEMA的sql和information_schema表, MySQL管理一些基础SQL语句, Changes in MySQL 5.7.2

    3.查看库表的最后mysql修改时间, 如果第一次新建的表可能还没有update_time,所以这里用了ifnull,当update_time为null时用create_time替代 select T ...

  2. Python已成为网络攻击的首选编程语言

    Python已成为网络攻击的首选编程语言 最新的调查数据表明,Python已经变成了世界上最热门的编程语言了,而Python的热门风也刮到了信息安全领域中.Python,摇身一变,也变成了黑客开发网络 ...

  3. 从win10家庭版/中文版升级到win10专业版

    发布时间:2015-8-4     很多同学在不了解win10系统版本的情况下,安装了win10家庭版/中文版,不管是win10家庭版,还是win10家庭中文版,或者是win10家庭单语言版,它们都是 ...

  4. OpenCV 脸部跟踪(2)

          前面一篇文章中提到,我们在一副脸部图像上选取76个特征点,以及这些特征点的连通性信息来描述脸部形状特征,本文中我们会把这些特征点映射到一个标准形状模型.       通常,脸部形状特征点能 ...

  5. Android中Intent的显示和隐式使用

    Android应用程序中组件之间的通信都少不了Intent的使用,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件, ...

  6. Memento 备忘录 快照模式 MD

    备忘录模式 简介 在不破坏封装的前提下,捕获一个对象的[内部状态],并在该对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存的状态. 角色: 发起人Originator:要被备份的成员,它提供 ...

  7. Dropwizard框架入门

    最近项目用到了Dropwizard框架,个人感觉还不错,那么这里就从他们官网入手,然后加上自己的实现步骤让大家初步了解这个框架. 官网对DW(Dropwizard)的定义是跨越了一个库和框架之间的界限 ...

  8. 两个JS对象怎样才能相等

    在JS中,两个对象如何才能相等?下面的两个 Alert,只有一个输出true.  在JS中如何才能构造出两个JS对象相等? var prop1 = {asd:{def:'abc'}}; var pro ...

  9. smtp发送带附件的邮件(直接将string类型结果保存为附件)

    该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好    MailMessage mmsg = new MailMessage();    mmsg.Subject = " ...

  10. 微信小程序中用户唯一ID的获取

    折腾到半夜,搞得挺兴奋,总结一下,免得忘了: 1.微信小程序直接获得的是一些简单信息,基本无用 2.用户唯一标识是openid,还有一个unionid是关联多个公众号之类情况下用,我不大关心 3.在g ...