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设置为空,否则将next设置为右侧相邻节点。

解决思路:

  一次对每个节点进行遍历,若左右子树不为空,则将左子树的next指向右子树,若该节点的next为空,则将右子树的next设置为空(看图分析)

  若该节点的next不为空,则指向与该节点同层次中相邻的右侧节点的左子树(看图分析)

  这里使用一个队列,将根节点先放入队列,从队列中取出一个节点node,node移除队列,node子树的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:
void connect(TreeLinkNode *root) {
if(root == NULL) return;
queue<TreeLinkNode *> q; root->next = NULL; q.push(root); while(!q.empty()){
TreeLinkNode *node = q.front();
q.pop(); if(node->left != NULL && node->right != NULL){
q.push(node->left);
q.push(node->right); node->left->next = node->right;
if(node->next == NULL)
node->right->next = NULL;
else{
TreeLinkNode *node_next = q.front();
node->right->next = node_next->left;
} } } }
};

Populating Next Right Pointers in Each Node 设置二叉树的next节点的更多相关文章

  1. 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

    题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...

  2. LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  3. Populating Next Right Pointers in Each Node I, II——生成next树

    1. Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

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

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

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

  6. [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  7. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  8. LEETCODE —— Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  9. LeetCode - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

随机推荐

  1. word中手动添加endnote的加载项

    用Endnote管理文献,在写作的同时插入引文,这对于写文章的朋友们来说太重要了.我今天遇到这个问题,花时间钻研了,觉得应该记录下来,相信也会方便大家.查了网上许多帖子依然不得解,可能是Word版本变 ...

  2. 基于Anaconda 安装 geatpy 和 tensorflow

    装了好久的第三方包终于成功了,暴风哭泣!!!总结一下 分两部分说: 一. 首先是在本地电脑windows系统下装: 首先安利一下这个包括各种 Genetic and Evolutionary Algo ...

  3. 修改VS项目的目标平台(目标框架)

    如果是正常的情况下.. 右键项目属性里就有修改的地方.. 可是有时候打开属性发现修改的下拉框是禁用的.. 这时候可以右键 "卸载项目" 编辑 .csproj 项目文件 在上方有个& ...

  4. 本地jar包 安装到本地仓库中的命令

    maven 项目 本地jar包 安装到本地仓库中去: 首先进入到该文件所在文件夹内 若不在直接绝对路径就可以.注意命令中的空格 mvn install:install-file  -Dfile=文件名 ...

  5. 【文档】六、Mysql Binlog版本

    binlog文件格式有以下几种: v1:用于3.23版本 v3:用于4.0.2到4.1版本 v4:用于5.0及以上版本 v2版本只在4.0.x版本中使用,目前已经不再支持了. 处理binlog的程序必 ...

  6. Cassandra概念学习系列之Windows里下载且安装配置Cassandra(最新的3.11.1版本)(图文详解)

    不多说,直接上干货!  最近我开始在windows环境中使用Cassandra,虽然在Cassandra站点的安装命令非常清楚和精简,我仍然在环境配置上遇到一些问题.所以我想为后来者分享下我的经验. ...

  7. Zip文件格式

    Overview This document describes the on-disk structure of a PKZip (Zip) file. The documentation curr ...

  8. 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较

    几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 来源   XFire VS Axis XFire是与Axis2 并列的新一代WebService平台.之所 ...

  9. strcpy和strncpy

    在c语言中,对于简单变量,如int型.double型,直接使用赋值符号“=”,即可完成赋值,如 int a=10: int b: b=a: 即可完成用a给b赋值. 但是对于字符串,这样赋值是不准确的. ...

  10. PTA (Advanced Level) 1019 General Palindromic Number

    General Palindromic Number A number that will be the same when it is written forwards or backwards i ...