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. 关闭WIN10的UAC/自动更新/杀毒软件(兼容WIN7/8/8.1)

    第一部分:禁用wuauserv服务以及关闭UAC(保存为DW10UD.REG) Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYS ...

  2. Oracle 11gR2 RAC Votedisk and OCR Diskgroup Recovery

    check votedisk and OCR [root@vzwc1 ~]# ocrcheck Status of Oracle Cluster Registry is as follows : Ve ...

  3. expect 批量监控主机

    [oracle@OAPRIMARY shell]$ cat expect.sh while read line do user=`echo $line | awk '{print $1}'` ip=` ...

  4. 模仿TMALL搜索,下拉提示 优化 用户keypress停顿200毫秒间隔时,在执行异步取数据操作 通过underscore的函数debounce来实现

  5. Codeforce 218 div2

    D 一开始想错了,试图用"前缀和-容量"来求从上层流下来了多少水",但这是错的,因为溢出可能发生在中间. 然后发现对于每层,溢出事件只会发生一次,所以可以用类似并查集的办 ...

  6. c语言else匹配问题

    #include <stdio.h> #include <stdlib.h> //实现 依次输入三个递增的数 然后正确输出 //为什么得不到我们想要的结果呢 这就是else匹配 ...

  7. 关于bootstrap--表格(tr的各种样式)

    只需要<tr class="active">就可以用active样式. 特别提示:除了”.active”之外,其他四个类名和”.table-hover”配合使用时,Bo ...

  8. Eclipse UML插件AmaterasUML的配置及使用

    AmaterasUML是个人觉得最好用的Eclipse UML插件,可以通过拖拽Java源文件,轻松生成类图结构,同时支持活动图.时序图和用例图.它的官方下载地址是:http://sourceforg ...

  9. html img Src base64 图片显示

    http://blog.csdn.net/knxw0001/article/details/10983605 大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: ...

  10. verilog 实现加法器

    半加器 如果不考虑来自低位的进位将两个1二进制数相加,称为半加. 实现半加运算的逻辑电路称为半加器. 真值表 逻辑表达式和 \begin{align}\notag s = a{b}' + {a}'b ...