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

这题首先想到的当然是用bfs来做,但是题目只允许使用常数量的额外空间,用queue肯定是无法实现的,那么可以采取先序遍历的方式,由于是完全二叉树,所以有左孩子那么就一定也有右孩子了,所以递归的时候注意这点就可以了。下面是代码:

 class Solution {
public:
void connect(TreeLinkNode *root) {
preorderTranversal(root);
} void preorderTranversal(TreeLinkNode *root)
{
if(!root || !root->left) return;
root->left->next = root->right;
if(root->next)
root->right->next = root->next->left;//这一步的连接应该注意一下
preorderTranversal(root->left);
preorderTranversal(root->right);
}
};

当然这题也可以使用非递归的方法来实现,非递归方法代码如下所示:

 /**
* 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 ;
while(root->left){
TreeLinkNode * tmpRoot = root;
tmpRoot->left->next = tmpRoot->right;
while(tmpRoot->next){
tmpRoot->right->next = tmpRoot->next->left;
tmpRoot = tmpRoot->next;
if(tmpRoot->left)
tmpRoot->left->next = tmpRoot->right;
}
root = root->left;
}
}
};

LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)的更多相关文章

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

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

  2. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. [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 ...

  4. [Leetcode][JAVA] Populating Next Right Pointers in Each Node II

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

  5. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  6. [Leetcode Week15]Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...

  7. Java for LeetCode 117 Populating Next Right Pointers in Each Node II

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

  8. [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 ...

  9. 【leetcode】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  10. 【leetcode】Populating Next Right Pointers in Each Node

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

随机推荐

  1. 阿里云免费SSL证书申请及配置过程centos7,Nginx

    1:进入购买,等待审核,审核通过后下在一个压缩包 ,里面包含两个文件一个 214979907780888.key 一个214979907780888.pem (如果不在阿里买,也可以在其它平台买,或者 ...

  2. OpenAI gym的建模思想

    一.强化学习问题需要描述那些内容 强化学习中最主要的两类对象是“个体”和“环境”,其次还有一些像“即时奖励”.“收获”.“状态”.“行为”.“价值”.“策略”.“学习”.“控制”等概念.这些概念把个体 ...

  3. Log4j详细配置解释

    原文地址:https://www.cnblogs.com/godtrue/p/6444158.html log4j(七)——log4j.xml简单配置样例说明 一:测试环境与log4j(一)——为什么 ...

  4. Python进阶(5)_进程与线程之协程、I/O模型

    三.协程 3.1协程概念 协程:又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存 ...

  5. ajax使用formdata 提交excel文件表单到rails解析

    .modal-body .container-fluid .row .col-md-12 1.下载模板文件 = link_to '模板文件' .row .col-md-12 = form_tag '' ...

  6. 建议42:使用pandas处理大型CSV文件

    # -*- coding:utf-8 -*- ''' CSV 常用API 1)reader(csvfile[, dialect='excel'][, fmtparam]),主要用于CSV 文件的读取, ...

  7. 20145239杜文超《网络对抗》- Web安全基础实践

    20145239杜文超<网络对抗>- Web安全基础实践 基础问题回答 (1)SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查 ...

  8. MySQL数据库基本操作(四)

    在进行查询之前,我们要先建好关系表,并往数据表中插入些数据.为查询操作做好准备. 五张关系表的创建: #创建并进入数据库: mysql> CREATE DATABASE `info`; Quer ...

  9. MongoDB快速入门(五)- Where子句

    RDBMS Where子句等效于MongoDB 查询文档在一些条件的基础上,可以使用下面的操作 操作 语法 示例 RDBMS等效语句 Equality {<key>:<value&g ...

  10. ssm文件上传下载比较详细的案例

    背景:ssm框架 接下来,我会介绍单文件上传,下载,多文件的上传,下载,使用ajax进行文件的上传下载,和普通的表单提交的文件上传下载. 只要做项目,总是少不了文件的操作,好了废话不多说,直接上代码! ...