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. [Re:从零开始的分布式] 0.x——分布式基础概念

    分布式的特点 1. 分布式 2. 对等性 3. 并发性 4. 缺乏全局时钟 5. 故障总是会发生 分布式环境的问题 1. 网络不可靠 2. 网络分区 3. 节点故障 CAP理论 一致性 可用性 分区容 ...

  2. lightgbm调参方法

    gridsearchcv: https://www.cnblogs.com/bjwu/p/9307344.html gridsearchcv+lightgbm cv函数调参: https://www. ...

  3. 用js制作简易计算器及猜随机数字游戏

    <!doctype html><html><head> <meta charset="utf-8"> <title>JS ...

  4. 如何快速将文本中的tab更换成逗号(图文详解)

    不多说,直接上干货! 现有一份数据如下. 下载日志数据并分析 到搜狗实验室下载用户查询日志 1) 介绍 搜索引擎查询日志库设计为包括约1个月(2008年6月)Sogou搜索引擎部分网页查询需求及用户点 ...

  5. Spring中线程池的应用

    多线程并发处理起来通常比较麻烦,如果你使用spring容器来管理业务bean,事情就好办了多了.spring封装了Java的多线程的实现,你只需要关注于并发事物的流程以及一些并发负载量等特性,具体来说 ...

  6. memcached 学习笔记 2

    原理 1 核心组件 Memcached有两个核心组件组成:服务端(ms)和客户端(mc). 首先mc拿到ms列表,并对key做hash转化,根据hash值确定kv对所存的ms位置. 然后在一个memc ...

  7. Kubernetes是什么

    目录 简介 主要概念: 总体结构 参考 Kubernetes概念 简介 kubernetes是一个Google开源的容器编排系统,用于自动部署,扩展和管理容器化应用程序. 随处运行:支持公有云,私有云 ...

  8. ORACLE: private ,dao中util执行规范,nextval计数把通过nextval插入但已删除的列也统计在内向后计数

    private DAO中的util.rs.sql都应该为private. 其中每个具体方法执行增删改查操作前打开数据库连接,操作完成后关闭数据库连接.操作要规范,不然易出错. nextval seq_ ...

  9. JAVA泛型——协变

    在上篇<JAVA泛型——基本使用>这篇文章中遗留以下问题,即将子类型Table或者也能添加到父类型Auction的泛型中,要实现这种功能必须借助于协变. 实验准备 现在在<JAVA泛 ...

  10. WCF-ServiceEndpoint的监听地址与监听模式

    ServiceEndpoint具有一个可读可写的ListenUri属性,该属性表示服务端终结点的物理监听地址,该地址默认和终结点逻辑地址一致(即ServiceEndpoint的Uri).对于客户端来说 ...