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

  方法一: constant extra space

/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( !root || ( !root->left && !root->right)) return ; root->left->next = root-> right;
if(root->next)
root->right->next = root->next->left;
connect(root->left);
connect(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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(NULL == root) return ;
queue<TreeLinkNode *> myqueue, tpqueue;
TreeLinkNode *p,*pre;
myqueue.push(root);
while(!myqueue.empty()){
pre = myqueue.front();myqueue.pop();
if(pre->left) tpqueue.push(pre->left);
if(pre->right) tpqueue.push(pre->right);
while(!myqueue.empty()){
p = myqueue.front();myqueue.pop();
if(p->left) tpqueue.push(p->left);
if(p->right) tpqueue.push(p->right);
pre->next = p;pre = p;
} myqueue.swap(tpqueue);
}
}
};

方法三: 方法一的非递归实现,符合题目的空间要求

class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(NULL == root) return;
TreeLinkNode *head = root;
TreeLinkNode *tp;
while(head->left){
tp = head->left;
while(head){
if(head->left)
head->left->next = head ->right;
if(head ->right && head->next)
head->right->next = head->next->left;
head = head ->next;
}
head = tp;
}
}
};

LeetCode_Populating Next Right Pointers in Each Node的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Qt调用VC++生成的动态链接库

    Qt如何调用VC++生成的动态链接库?假设当前有VC++编译器生成的动态库文件testdll.h,testdll.lib和testdll.dll. testdll.h文件源码如下: #ifdef TE ...

  2. translate函数说明

    TRANSLATE(expr, from_string, to_string) from_string 与 to_string 以字符为单位,对应字符一一替换. SQL> SELECT TRAN ...

  3. 数字积分法DDA(DDA(Digital Differential Analyzer)

    数字积分法DDA(DDA(Digital Differential Analyzer)    数字积分法又称数字微分分析法DDA(Digital differential Analyzer),是在数字 ...

  4. Centos 6.5中安装后不能打开emacs的问题

    问题的发现过程: 安装了最新的centos版本后发现居然打不开emacs,然后在终端中输入emacs后还是不能打开,出现了下面的提示: emacs: error while loading share ...

  5. c指针点滴1

    #include <stdio.h> #include <stdlib.h> void main() { ; int *p = &num;//&num是一个地址 ...

  6. 【转】FLV视频封装格式详解

    Overview Flash Video(简称FLV),是一种流行的网络格式.目前国内外大部分视频分享网站都是采用的这种格式. File Structure 从整个文件上开看,FLV是由The FLV ...

  7. maven编写主代码与测试代码

    3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目主代码位于src/main/ja ...

  8. Windows Server 2008搭建域控制器《转载51CTO.com》

    Windows Server 2008搭建域控制器 引入 在小型网络中,管理员通常独立管理每一台计算机,如最为常用的用户管理.但当网络规模扩大到一定程度后,如超过 10 台计算机,而每台计算机上有 1 ...

  9. 搭建LAMP架构

    1. 为什么下载源码包需要到官网上去下载?简单说就是为了安全,如果是非官方下载的源码包,有可能被别有用心的人动过手脚. 2. 64位机器是否可以安装32位rpm包?64位机器是否可以安装32位的mys ...

  10. DotNet程序汉化过程--SnippetCompiler准确定位

    开篇前言 上一篇简单介绍了一下怎么汉化.Net程序,但那也仅仅是最基础的工作,要想汉化好一款软件基础我们得做扎实了,但是对于一些需要技巧的也不能不会啊,这一篇就介绍一下怎么准确定位字符串. 主要使用工 ...