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 & ...
 
随机推荐
- OrCAD16.6中对比两份DSN文件的方法
			
OrCAD16.6中对比两份改版前后DSN文件的方法 两种方法: (1)第一种用软件对比netlist (2)用orcad自带的对比功能 一.将两份要对比的原理图都生成orTelesis.dll格式的 ...
 - atitit.bsh BeanShell 的动态脚本使用java
			
atitit.bsh BeanShell 的动态脚本使用java 1.1. BeanShell是一个小巧免费的JAVA源码解释器 ,支持对象式的脚本语言特性,亦可嵌入到JAVA源代码中. 亦可嵌入到J ...
 - linux的用户、群组
			
1. 用户及passwd文件 1) 掌握/etc/passwd文件的功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 2) /etc/pa ...
 - 自定义Spring Shell
			
目录 概述 自定义内置命令 禁用内置命令 覆盖内置命令 自定义命令提示符 自定义命令行选项行为 自定义参数转换器 概述 官网:https://projects.spring.io/spring-she ...
 - 使用mescroll实现上拉加载与下拉刷新
			
现在上拉加载与下拉刷新几乎已经是移动端必备功能之一了,自己实现一个太麻烦,但是好用的插件又非常少.之前看到网上很多人都在用iScroll,于是也尝试用它做了几个DEMO,但或多或少都有一些问题,比如这 ...
 - oracle高性能的SQL语句的写法
			
1.当多表查询的时候,把数据量小的表放在最后面,ORACLE会把最后面的表当作基础表,因为表间连接时,最右边的表会被放到嵌套循环的最外层.最外层的循环次数越少,效率越高. 2.Oracle采用自下而上 ...
 - Vim使用个人心得
			
个人最近在Windows上使用gVim 1.移动光标 h,j,k,l 键为左,下,上,右,方向键,控制光标移动,插入状态下不可用,插入状态下,按V键进入查看状态,可使用. 2.进入编辑模式:按 i 键 ...
 - IDEA 去掉 ALT + / 自动补全
			
取消掉Alt + / 自动补全 setting -> keymap -> main menu -> code -> completion -> cyclic Expand ...
 - Mac OS X 安装Ruby
			
安装CocoaPods第一步 起因:重装系统后需要重新安装CocoaPods网上搜了下发现很多都过时了,已经不能用了.而且taobao Gems源已经停止服务,现在有ruby-china提供服务 PS ...
 - 从零开始学android -- Service
			
废话不多说了,Service是四大组件之一,是一个后台处理长时间运行在主线程不需要依赖ui界面显示的应用组件,切记不能在service中做耗时操作,会阻塞主线程,要做也要在service中开个子线程做 ...