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. 实现ECMAScript的引擎

    list of ECMAScript engines From Wikipedia, the free encyclopedia     An ECMAScript engine is a progr ...

  2. GSM Channel Mode Modify和Channel Mode Modify Acknowledge信令

    最近研究了下如何通过GSM Channel Mode Modify和Channel Mode Modify Acknowledge信令,获知GSM终端支持的data Rate   思路与原理: • I ...

  3. linux下core文件设置(转)

    在程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息).使用gdb来查看core文件,可以指示出导致程序出错的代码所在文件和行数. 1.core文件的生成开 ...

  4. win10 下安装、配置、启动mysql

    1.下载http://dev.mysql.com/downloads/mysql/ 2.Community > MySQL Community Server 3.Other Downloads: ...

  5. WebService- 使用 CXF 开发 SOAP 服务

    选框架犹如选媳妇,选来选去,最后我还是选了“丑媳妇(CXF)”,为什么是它?因为 CXF 是 Apache 旗下的一款非常优秀的 WS 开源框架,具备轻量级的特性,而且能无缝整合到 Spring 中. ...

  6. Android静态变量使用陷阱

    静态变量大家再熟悉不过了,本来没什么好重复的.事情起因是这样的,最近测试那边反应正在做的一个产品总是莫名其妙的显示不出某些数据,甚至闪退崩溃,仔细查了几遍发现没什么问题,最后百般周折发现在那部测试机上 ...

  7. C++11中正則表達式測试

    VC++2010已经支持regex了, 能够用来编译下述代码. #include <string> #include <regex> #include <iostream ...

  8. 程序员的绘图利器 — Gnuplot

      介绍 Gnuplot is a command-line program that can generate two- and three-dimensional plots. It is fre ...

  9. C#用注册表开机自动启动某某软件

    代码如下: public static void chkAutoRun(bool isRun) { if (isRun)//开机自动启动 { try { RegistryKey runKey = Re ...

  10. 『转载』Debussy快速上手(Verdi相似)

    『转载』Debussy快速上手(Verdi相似) Debussy 是NOVAS Software, Inc(思源科技)发展的HDL Debug & Analysis tool,这套软体主要不是 ...