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. hdu 2871 Memory Control(线段树)

    题目链接:hdu 2871 Memory Control 题目大意:模拟一个内存分配机制. Reset:重置,释放全部空间 New x:申请内存为x的空间,输出左地址 Free x:释放地址x所在的内 ...

  2. Android开发之Serializable 和 Parcelable的差别(源码分享)

    android 中自己定义的对象序列化的问题有两个选择一个是Parcelable,另外一个是Serializable. 一 序列化原因: 1.永久性保存对象.保存对象的字节序列到本地文件里. 2.通过 ...

  3. java统计中英文字数 Java问题通用解决代码

    http://yangchao20020.blog.163.com/blog/static/483822472011111635424751/   这个不适用于新浪微博字数的统计,结果有差别,若需要可 ...

  4. 运行./cpp.sh,显示command not found

    首先运行ls -l 查看这个文件的属性是否可执行drwxrwxrwx对当前用户必须具有可执行权限(即含有x符号)如果没有可以运行chmod 777 cpp.sh 添加可执行权限

  5. ASP.NET CORE RAZOR :向 Razor 页面应用添加模型

    本文来自:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/model 在本部分中将添加用于管理数据库中的电影的类. ...

  6. SRIO常用缩写

    HELLO:Header Encoded Logical Layer Optimized (HELLO) format FTYPE:format type TTYPE:transaction type ...

  7. JavaScript的toString()

    JavaScript toString() 方法 JavaScript Boolean 对象 定义和用法 toString() 方法可把一个逻辑值转换为字符串,并返回结果. 语法 booleanObj ...

  8. 高度平衡树 -- AVL 树

    Scheme 的表达, 优雅. #lang scheme ( define nil '() ) ( define ( root tree )( car tree ) ) ( define ( left ...

  9. left join,right join用法简介

    方法一(推荐): select a.man_id,man_name,d.sex_name,zw_name,c.money from man as a left join zw as b on a.zw ...

  10. 什么是scale up和scale out

    Scale Out(也就是Scale horizontally)横向扩展,向外扩展Scale Up(也就是Scale vertically)纵向扩展,向上扩展无论是Scale Out,Scale Up ...