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. Rsync服务介绍与配置

    Rsync 简要介绍 rsync 是一个用于增量文件传输的开源工具,不得不说,rsync简直是不同服务器间传输文件.同步文件的利器.与FTP相比,它具有非常简单的安装和配置方法.而且,rsync可以只 ...

  2. Coursera课程《Python数据结构》中课件

    You can access the Google Drive containing all of the current and in-progress lecture slides for thi ...

  3. 在Ubuntu 12.04 桌面上设置启动器(快捷方式)

    在Ubuntu 12.04 桌面上设置启动器(快捷方式)过程讲解: 如下图所示,Eclipse 和 SQLDeveloper 都可以直接双击打开,这些应用程序的启动器都在 /usr/share/app ...

  4. @Java类加载的过程

    前言 我们写的源程序.java文件经过编译后成为了.class字节码文件,.class文件中描述了类的各种信息,最终都需要加载到虚拟机(JVM)之后才能运行和使用.而虚拟机如何加载这些.class文件 ...

  5. 使用Java、Matlab画多边形闭合折线图

    由于写论文要将“哈密顿回路问题(TSP)”的求解中间结果表示出来,查了一下使用程序画多边形图形.现在在总结一下,这个图是“由给定节点首尾相连的”闭合多边形. 1.使用matlab作闭合多边形图 没有找 ...

  6. windows server 2008 远程桌面(授权、普通用户登录)~ .

    大家好,因公司上ERP系统,用户端需要远程到服务器,但大家都知道微软默认只有2个,所以没有办法达到我公司的要求. 在网上找了很久也没有找到合适的文章,要不就这里说一点,那里说一点,没有一个全的,还有很 ...

  7. RS开发值提示默认为当前月

    在报表的开发过程中,按月查询数据,但是由于数据仓库中涉及多年历史数据,而用户最关心的却是最近的数据,针对这个情况.当用户第一次点击报表想看到的就是当前月的数据,那么如何去做呢? 下面用一个小例子来实战 ...

  8. c语言 多重排序

    经过了两天,终于会了. #include<stdio.h> #include<string.h> struct A { char name[100]; int gread; } ...

  9. xhEditor在线编辑器使用实例

    使用xhEditor的最大好处就是不用去处理烦人的HTML标签问题,研究了一天,记录备用 前台HTML: <%@ Page Language="C#" AutoEventWi ...

  10. uva 11181 - Probability|Given(概率)

    题目链接:uva 11181 - Probability|Given 题目大意:有n个人去超市买东西,给出r,每个人买东西的概率是p[i],当有r个人买东西的时候,第i个人恰好买东西的概率. 解题思路 ...