【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
来源:https://leetcode.com/problems/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 7After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
(二)解题
题目大意:定义一个新的二叉树结构,增加一个新指针next指向该节点右侧相邻的节点。
解题思路:考虑到要指向右侧相邻的节点,可以采用层序遍历的方式来求解
将每一层的节点用next连接起来即可!
层序遍历可以参考:【一天一道LeetCode】#102. Binary Tree Level Order Traversal
废话不说,看AC代码:
/**
* 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) {
if(root==NULL) return;
queue<TreeLinkNode *> myque;
myque.push(root);
while(!myque.empty())
{
queue<TreeLinkNode *> temp_que;
TreeLinkNode *pre = NULL;
while(!myque.empty())
{
TreeLinkNode *temp = myque.front();
myque.pop();
if(pre==NULL) pre = temp;//相当于每一层的头节点
else {
pre->next = temp;//用next指针连接每一层的节点
pre = temp;
}
if(temp->left!=NULL) temp_que.push(temp->left);//下一层
if(temp->right!=NULL) temp_que.push(temp->right);//下一层
}
pre->next=NULL;//最后一个节点的next需要指向NULL
myque=temp_que;//进入下一层操作
}
}
};
【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node的更多相关文章
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针
You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...
- leetcode 116 Populating Next Right Pointers in Each Node ----- java
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- Java for LeetCode 116 Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode] 117. 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 ...
- 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】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
随机推荐
- 在java中String类为什么要设计成final
在java中String类为什么要设计成final? - 胖胖的回答 - 知乎 https://www.zhihu.com/question/31345592/answer/114126087
- python webdriver环境搭建
一.准备安装包 1.下载python 2.下载setuptools 3.下载pip 二.windows环境安装 1.安装python,建议选择python2.7.5版本. 2.安装setuptools ...
- 54. Spiral Matrix(中等)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- Python中如何自定义一个计时器
import time as t class MyTimer(): # 初始化构造函数 def __init__(self): self.prompt = "未开始计时..." s ...
- Mac下配置远程Linux 服务器SSH密钥认证自动登录
1. 在本地机器创建公钥 打开万能的终端,执行如下命令,无视一切输出,一路欢快地回车即可. ssh-keygen -t rsa -C 'your email@domain.com' -t 指定密钥类型 ...
- Java 读取Excel文件
https://www.cnblogs.com/wwzyy/p/5962076.html 先把上面的参考博客看了,如果会导入包的话,下面的教程就直接忽略emm 这时候,你应该把jar包下载 ...
- Android简易实战教程--第四十五话《几种对话框》
Android中提供了各种原生的对话框,在使用简单的功能的时候,还不比考虑自定义,使用原生的也能完成功能.本篇简单小案例就介绍三种对话框. 还是直接上代码吧: 布局中三个点击事件的按钮: <Li ...
- High Executions Of Statement "delete from smon_scn_time..."
In this Document Symptoms Cause Solution APPLIES TO: Oracle Database - Enterprise Edition - Ve ...
- 2017京东校招面试回忆(已成功拿到offer)
一面 24日 晚上5:30-6:40 1 先说自己熟悉的领域 2 list的实现有什么? arraylist1.6 1.7区别 底层 linkedlist 底层是怎么实现的 单向还是双向 ...
- 高通开发笔记---Yangtze worknote
点击打开链接 1. repo init -u git://review.sonyericsson.net/platform/manifest -b volatile-jb-mr1-yangtze 2. ...