http://www.geeksforgeeks.org/connect-nodes-at-same-level-with-o1-extra-space/

recursive:

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
using namespace std; struct node {
int data;
struct node *left, *right, *next;
node() : data(), left(NULL), right(NULL), next(NULL) { }
node(int d) : data(d), left(NULL), right(NULL), next(NULL) { }
}; node *getnext(node *root) {
node *next = root->next;
while (next) {
if (next->left) return next->left;
if (next->right) return next->right;
next = next->next;
}
return NULL;
} void _connect(node *root) {
if (!root) return;
if (root->next) _connect(root->next);
if (root->left) {
if (root->right) {
root->left->next = root->right;
root->right->next = getnext(root);
}
else root->left->next = getnext(root);
_connect(root->left);
}
else if (root->right) {
root->right->next = getnext(root);
_connect(root->right);
}
else _connect(root->next);
} void connect(node *root) {
if (!root) return;
root->next = NULL;
_connect(root);
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->right->right = new node();
connect(root);
cout << root->data << "->" << (root->next? root->next->data : -) << endl;
cout << root->left->data << "->" << (root->left->next? root->left->next->data : -) << endl;
cout << root->right->data << "->" << (root->right->next? root->right->next->data : -) << endl;
cout << root->left->left->data << "->" << (root->left->left->next? root->left->left->next->data : -) << endl;
cout << root->right->right->data << "->" << (root->right->right->next? root->right->right->next->data : -) << endl;
return ;
}

iterative(better)

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
using namespace std; struct node {
int data;
struct node *left, *right, *next;
node() : data(), left(NULL), right(NULL), next(NULL) { }
node(int d) : data(d), left(NULL), right(NULL), next(NULL) { }
}; node *getnext(node *root) {
node *next = root->next;
while (next) {
if (next->left) return next->left;
if (next->right) return next->right;
next = next->next;
}
return NULL;
} void connect(node *root) {
if (!root) return;
root->next = NULL;
while (root) {
node *p = root;
while (p) {
if (p->left) {
if (p->right) p->left->next = p->right;
else p->left->next = getnext(p);
}
if (p->right) p->right->next = getnext(p);
p = p->next;
}
if (root->left) root = root->left;
else if (root->right) root = root->right;
else root = root->next;
}
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->right->right = new node();
connect(root);
cout << root->data << "->" << (root->next? root->next->data : -) << endl;
cout << root->left->data << "->" << (root->left->next? root->left->next->data : -) << endl;
cout << root->right->data << "->" << (root->right->next? root->right->next->data : -) << endl;
cout << root->left->left->data << "->" << (root->left->left->next? root->left->left->next->data : -) << endl;
cout << root->right->right->data << "->" << (root->right->right->next? root->right->right->next->data : -) << endl;
return ;
}

Data Structure Binary Tree: Connect nodes at same level using constant extra space的更多相关文章

  1. Data Structure Binary Tree: Populate Inorder Successor for all nodes

    http://www.geeksforgeeks.org/populate-inorder-successor-for-all-nodes/ #include <iostream> #in ...

  2. Data Structure Binary Tree: Lowest Common Ancestor in a Binary Tree

    http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/ #include <iostream> #in ...

  3. Data Structure Binary Tree: Print ancestors of a given binary tree node without recursion

    http://www.geeksforgeeks.org/print-ancestors-of-a-given-binary-tree-node-without-recursion/ #include ...

  4. Data Structure Binary Tree: Convert a given Binary Tree to Doubly Linked List

    http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ #include &l ...

  5. Data Structure Binary Tree: Iterative Postorder Traversal

    http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/ #include <iostream> #i ...

  6. Data Structure Binary Tree: Largest Independent Set Problem

    http://www.geeksforgeeks.org/largest-independent-set-problem/ #include <iostream> #include < ...

  7. Data Structure Binary Tree: Morris traversal for Preorder

    http://www.geeksforgeeks.org/morris-traversal-for-preorder/ #include <iostream> #include <v ...

  8. Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals

    http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-travers ...

  9. Data Structure Binary Tree: Boundary Traversal of binary tree

    http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/ #include <iostream> #include & ...

随机推荐

  1. Oracle 导入导出数据库

    imp userid=yrsuser/yrsuser2587 fromuser=yrsuser touser=yrsuser file=E:\yrs.dmp exp userid=yrsuser/yr ...

  2. src-resolve: 无法将名称 'extension' 解析为 'element declaration' 组件。

    activiti流程部署时,出现“src-resolve: 无法将名称 'extension' 解析为 'element declaration' 组件.”错误. 出错原因:项目所在路径中有中文.

  3. MongoDB基本命令使用

    成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...

  4. SVN版本控制图标未显示或显示异常

    TortoiseSVN下载的文件和文件夹如果缺失了那些花花绿绿的状态小图标,很容易逼死某些强迫症患者,更何况这些小图标用处多多 接下来我会逐步展示从常规到非常规的一系列解决方案(不包括重装重启这一类) ...

  5. 链表的艺术——Linux内核链表分析

    引言: 链表是数据结构中的重要成员之中的一个.因为其结构简单且动态插入.删除节点用时少的长处,链表在开发中的应用场景许多.仅次于数组(越简单应用越广). 可是.正如其长处一样,链表的缺点也是显而易见的 ...

  6. Squid 启动/停止/重载配置文件 命令

    当你的 squid.conf 配置文档按照你的想法修改完以后,启动 squid 之旅就开始了. Squid安装设试命令: 1,初始化你在 squid.conf 里配置的 cache 目录 #/usr/ ...

  7. IIS 下 部署nodejs 使用反向代理

    目标服务器:targetServer 配置反向代理的服务器:reveseProxServer iis应该是iis7及以上版本,才可以. 1.确定最终访问的网址:比如www.baidu.com  .ww ...

  8. (三)spark算子 分为3大类

    ation算子通过sparkContext执行提交作业的runJob,触发rdd的DAG执行 (foreach) foreach(f) 会对rdd中的每个函数进行f操作,下面的f操作就是打印输出没有元 ...

  9. 解决UISlider滑块不灵敏

    由于UI给的thumbImage图片过小,默认UISlider开始拖动的手势范围只有thumbImage的大小之内. 为了解决这个问题需要创建一个子类继承于UISlider.重写其中的方法: - (C ...

  10. man page及info page用法

    Linux系统的在线求助man page与info page 先来了解一下Linux有多少命令呢?在文本模式下,你可以直接按下两个[Tab]按键,看看总共有多少命令可以让你用? [vbird@www ...