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. 系统性能模块psutil

    psutil是一个跨平台库,能够轻松实现获取系统运行的进程和系统利用率(包括cpu.内存.磁盘.网络等)信息.它主要用于系统监控,分析和限制系统资源及进程的管理.它实现了同等命令行工具提供的功能,如p ...

  2. 前端基础之Bootstrap介绍

    bootstrap简介 http://v3.bootcss.com/ Bootstrap优点:  下载:  Bootstrap引入 <meta name="viewport" ...

  3. 使用jQuery方法做任务左右栏移动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Linux基本命令 vim命令(一)

    vim的三种工作模式 命令模式.输入模式和编辑模式的相互转换,如图 命令模式:使用 Vim 编辑文件时,默认处于命令模式.在此模式下,可以使用上.下.左.右键或者 k.j.h.l 命令进行光标移动,还 ...

  5. Java数据类型 及 转换原则

    一.数据类型分类:主要分为 基本类型.引用类型两大类: 二.基本类型 转换原则 1.类型转换主要在在 赋值.方法调用.算术运算 三种情况下发生. a.赋值和方法调用 转换规则:从低位类型到高位类型自动 ...

  6. comet4j开发指南

    http://blog.csdn.net/majian_1987/article/details/8489738 好多朋友反映,因排版问题,文章部分边缘内容无法查看.尝试过排版,但仍无法显示正常,所以 ...

  7. 编译android源码m、mm、mmm命令的使用

    http://blog.163.com/zz_forward/blog/static/212898222201442873435471/ gcc怎么查看它的默认include路径和库的路径呢? //- ...

  8. Nginx配置中last和break及permanent和redirect的区别

    一.不写last和break 流程就是依次执行这些rewrite rewrite break - url重写后,直接使用当前资源,不再执行location里余下的语句,完成本次请求,地址栏url不变 ...

  9. RSA签名 python PHP demo 例子

    python RSA+MD5签名demo: #!/usr/bin/env python2.7 #coding:utf-8 import base64 from Crypto.PublicKey imp ...

  10. 关于Kinect音频开发的探究

    1.笔者在<Kinect体感程序设计入门>(王森著)的这本书中看到可以使用powershell和COM对象无缝整合,轻松的使用windows系统自带的语音合成功能. 步骤:•打开进入pow ...