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. 如何修改myeclipse中web项目的工作路径或默认路径

    如何修改myeclipse中web项目的工作路径或默认路径 博客分类: J2EE开发技术指南   安装好myeclipse后,第一次启动myeclipse时,都会弹出会弹出Workspace Laun ...

  2. java网络编程(2)InetAddress 类及udp协议

    InetAddress 类 JDK中为开发网络应用程序提供了java.net包,该包下的类和接口差点儿都是为网络编程服务的. InetAddress:用于描写叙述IP地址的对象 InetAddress ...

  3. warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'.

    'matching'参数是 git 1.x 的默认行为,其意是如果你执行 git push 但没有指定分支,它将 push 所有你本地的分支到远程仓库中对应匹配的分支. 而 Git 2.x 默认的是 ...

  4. linux 时间与本地时间不对应解决办法

    date -s '2015-01-22 13:00:22' #设置时间 clock -w #写入mac操作系统中 -------------------------------------date - ...

  5. 打造一个高逼格的android开源项目——小白全攻略 (转)

    转自:打造一个高逼格的android开源项目 小引子 在平时的开发过程中,我们经常会查阅很多的资料,最常参考的是 github 的开源项目.通常在项目的主页面能看到项目的简介和基本使用,并且时不时能看 ...

  6. 搜索maven的库中某个支持库的的最新版本

    首先放网址(建议挂个vpn): maven库中心:http://search.maven.org/ jcenter库中心:https://bintray.com/bintray/jcenter 接下来 ...

  7. ubuntu 及 postgredql 安装配置小坑摘录

    ubuntu 16.04.1 安装 Ubuntu Server 16.04.1安装配置图解教程,按教程修改局域网static IP 开启sftp必须 解决SSH服务拒绝密码,之后才能欢乐地使用file ...

  8. 查找 TextBox 对象中非法数据的示例

    private void GetErrors(StringBuilder sb, DependencyObject obj){ foreach (object child in LogicalTree ...

  9. sublime中如何安装vue.js插件,并使代码高亮显示

    前提概要: sublime的下载地址:http://www.sublimetext.com/ notepad++下载地址:https://notepad-plus-plus.org/ .vue的文件在 ...

  10. hdu4847:Wow! Such Doge!(字符串匹配)

    题目:hdu4847:Wow! Such Doge! 题目大意:在给出的段落里面找出"doge"出现的次数.大写和小写都能够. 解题思路:字符串匹配问题,能够在之前将字母都转换成统 ...