【LeetCode】115. Populating Next Right Pointers in Each Node (2 solutions)
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)的更多相关文章
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【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 ...
- 【LeetCode】116. Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
- 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 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 ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- 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 ...
- 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 ...
随机推荐
- Centos下运行cp命令式提示略过目录
今天在复制一个目录到还有一个目录的时候cp ./res /usr 的时候出现了问题,提示我的是: cp略过了目录 后来我找了一下 在网上search了一下CP命令的使用方法: CP命令 该命令的功能是 ...
- 全局安装 Vue cli3 和 继续使用 Vue-cli2.x
官方链接:https://cli.vuejs.org/zh/guide/installation.html 1.安装Vue cli3 关于旧版本 Vue CLI 的包名称由 vue-cli 改成了 @ ...
- 自适应图片宽度的jQuery焦点幻灯轮播代码
自适应图片宽度的jQuery焦点幻灯轮播代码 注意要1.7.2版本的jq才支持点击后显示点击的图片,不然就是一直顺序播放 演示 XML/HTML Code <div id="sli ...
- TreeListControl 不同类别的行使用不同的数据模板
- [Bash] Create Aliases in .bash_profile for Common Bash Commands
.bash_profile is a file that bash invokes (or more technically sources) before the start of a new ba ...
- 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
两种请求在请求的Header不同,Ajax 异步请求比传统的同步请求多了一个头参数 1. 传统同步请求参数 accept text/html,application/xhtml+xml,applica ...
- centos+apache+mod_ssl
参考网站:配置Apache建立openssl证书实现SSL访问:http://blog.51yip.com/apachenginx/958.html用ca工具生成证书的方法:http://hi.bai ...
- 不兼容:不支持SCSI硬盘
获取机器硬件失败,可能你使用了SCSI硬盘,请更换一台主机进行安装 聚生网管2.11版本不支持scsi硬盘. 终于体会到了不兼容的麻烦了.
- redis sentinel(哨兵机制)部署(Windows下实现)
另外参考:http://www.cnblogs.com/LiZhiW/p/4851631.html 一.准备条件 1.操作系统:win7 2.redis版本:redis-2.8.19 二.下载Redi ...
- Struts2(二)action的三种方式
一.普通java类 package com.pb.web.action; /* * 创建普通的java类 */ public class HelloAction1 { public String ex ...