Data Structure Binary Tree: Connect nodes at same level using constant extra space
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的更多相关文章
- Data Structure Binary Tree: Populate Inorder Successor for all nodes
http://www.geeksforgeeks.org/populate-inorder-successor-for-all-nodes/ #include <iostream> #in ...
- 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 ...
- 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 ...
- 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 ...
- Data Structure Binary Tree: Iterative Postorder Traversal
http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/ #include <iostream> #i ...
- Data Structure Binary Tree: Largest Independent Set Problem
http://www.geeksforgeeks.org/largest-independent-set-problem/ #include <iostream> #include < ...
- Data Structure Binary Tree: Morris traversal for Preorder
http://www.geeksforgeeks.org/morris-traversal-for-preorder/ #include <iostream> #include <v ...
- 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 ...
- Data Structure Binary Tree: Boundary Traversal of binary tree
http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/ #include <iostream> #include & ...
随机推荐
- C# 指南之装箱与拆箱
基础 1.值类型 1.1 在栈上分配内存,在声明时初始化才能使用,不能为null. 1.2 值类型超出作用范围系统自动释放内存. 1.3 主要由两类组成:结构,枚举 结构分为以下几类 1.整形(Sby ...
- IAR模板--怎样在IARproject中创建和使用模板
怎样在IARproject中创建和使用模板 1.编辑和使用模板的方式: 路径为:Edit -> Code Templates -> Edit Templates 例如以下图: water ...
- Java和C++ 比較
总体差别 1. C/C++是直接执行在机器上(编译后为机器码),而java编译后产生*.class文件(字节码)是执行在java虚拟机上在(JVM),经过JVM解译(机器码)再放到真实机器上执行. J ...
- qtav----ffmeg在ubuntu和win10上的编译和运行
最近在windows上和ubuntu上都安装了qtav并且通过了编译测试,实测播放中英文的视频文件功能正常,有图像有声音. 大致情况是,操作系统ubuntu: wkr@sea-X550JK:~$ ca ...
- Springboot Maven 多模块项目中 @Service跨模块引用失败的问题
子模块中引用另一个子模块中的Service, @Autowired失败. 添加了模块之间的依赖没解决. 组以后在启动类上加上 @SpringBootApplication(scanBasePackag ...
- nl 命令
nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...
- 一个方便的图片载入框架——ImageViewEx
我的博客:http://mrfufufu.github.io/ 一.前言 近期在整理项目中的一些代码,以备即将开展的新项目中使用,刚刚整理到一个图片载入的 lib.用起来很的简单,和 picasso ...
- CardView的具体使用方法(转)
转载自:CardView的具体使用方法 因为学习做此记录方便查找使用 今天主要是CardView的用法,CardView是在安卓5.0提出的卡片式控件.首先介绍一下它的配置. 在gradle文件下添 ...
- 多媒体开发之---开源库ffmeg的log之子解析
用了ffmeg快两年了,对其中的log甚是感兴趣,今天在做8148项目是,解读h264结构,看了<毕-新一代视频压缩编码标准h246> ,在第六章中的重排序里面看到了好熟悉的4x4矩阵zi ...
- Linux 技巧:让进程在后台运行的可靠方法
原文链接:http://www.ibm.com/developerworks/cn/linux/l-cn-nohup/ 想让进程在断开连接后依然保持运行?如果该进程已经开始运行了该如何补救? 如果有大 ...