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 <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; node *_convert(node *root) {
if (!root) return NULL;
if (root->left) {
node *left = _convert(root->left);
while (left->right) left = left->right;
left->right = root;
root->left = left;
}
if (root->right) {
node *right = _convert(root->right);
while (right->left) right = right->left;
right->left = root;
root->right = right;
}
return root;
} node *convert(node *root) {
if (!root) return NULL;
root = _convert(root);
while (root->left) root = root->left;
return root;
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
node *head = convert(root);
while (head) {
cout << head->data << " ";
head = head->right;
}
return ;
}
http://www.geeksforgeeks.org/convert-a-given-binary-tree-to-doubly-linked-list-set-2/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void fixpre(node *root) {
static node *pre = NULL;
if (!root) return;
fixpre(root->left);
root->left = pre;
pre = root;
fixpre(root->right);
} node *fixnext(node *root) {
node *pre = NULL;
while (root && root->right) root = root->right;
while (root && root->left) {
pre = root;
root = root->left;
root->right = pre;
}
return root;
} node *convert(node *root) {
if (!root) return NULL;
fixpre(root);
return fixnext(root);
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
node *head = convert(root);
while (head) {
cout << head->data << " ";
head = head->right;
}
return ;
}
http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-linked-list-set-3/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void convert(node *root, node *&head) {
if (!root) return;
static node *pre = NULL;
convert(root->left, head);
if (pre == NULL) head = root;
else {
root->left = pre;
pre->right = root;
}
pre = root;
convert(root->right, head);
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
node *head = NULL;
convert(root, head);
while (head) {
cout << head->data << " ";
head = head->right;
}
return ;
}
Data Structure Binary Tree: Convert a given Binary Tree to Doubly Linked List的更多相关文章
- Data Structure Binary Tree: Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
http://www.geeksforgeeks.org/convert-an-arbitrary-binary-tree-to-a-tree-that-holds-children-sum-prop ...
- 【转】Senior Data Structure · 浅谈线段树(Segment Tree)
本文章转自洛谷 原作者: _皎月半洒花 一.简介线段树 ps: _此处以询问区间和为例.实际上线段树可以处理很多符合结合律的操作.(比如说加法,a[1]+a[2]+a[3]+a[4]=(a[1]+a[ ...
- Convert a given Binary Tree to Doubly Linked List
The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-li ...
- [geeksforgeeks] Convert a given Binary Tree to Doubly Linked List
http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Bin ...
- [LeetCode] Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...
- Convert Binary Search Tree to Doubly Linked List
Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary s ...
- LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
原题链接在这里:https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ 题目: C ...
- 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表
[抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...
随机推荐
- 【原创】Loadrunner使用json格式请求数据并参数化
(2015-04-10 16:10:41) 转载▼ 标签: loadrunner json 参数化 web_custom_request 分类: 性能测试 请求自定义的http文件用函数:web_cu ...
- ubuntu下设置静态ip
直接修改系统配置文件ubuntu的网络配置文件是:/etc/network/interfacesubuntu命令行修改网络配置方法前面auto eth?,让网卡开机自动挂载. 为网卡配置静态IP地址 ...
- brew Error: Formulae found in multiple taps
Mac PHP brew install php56-apcu Error: Formulae found in multiple taps: * homebrew/php/php56-apcu * ...
- java web 通配符* ? $1 $2 $3
匹配通配符 * 匹配0-n个字符,但不包括“/”.即,“*”只匹配一级目录或文件中的零个或多个字符. ** 匹配0-n个字符,包括“/”.即,“**”匹配多级目录或文件. ? 匹配0-1个字符,但不包 ...
- 0x00 使用Ant 设置项目
1. Ant 简介: Ant 是一款广泛使用的流行的开源构建工具,它用Java语言编写. 2.Ant官网: Ant官网:http://ant.apache.org/ 3.设置环境变量: 新建 Vari ...
- ACM算法整理(不断补充ing)
动态规划 1.背包问题 (1)01背包 ,n) DFR(v,V,C[i]) F[v]=max(F[v],F[v-C[i]]+W[i]); } //初始化时 //若背包不一定装满F全初始化为0 //若装 ...
- eventfd
#include <sys/eventfd.h> int eventfd(unsigned int initval, int flags); eventfd() creates an &q ...
- js-音乐播放器,播放|暂停|滑块的功能
音乐播放器,播放|暂停|滑块的功能 document.addEventListener('DOMContentLoaded', function loaded(event) { var audio = ...
- mac Xvim 语法高亮
步骤1: cp /usr/share/vim/vimrc ~/.vimrc 先复制一份vim配置模板到个人目录下 注:redhat 改成 cp /etc/vimrc ~/.vimrc 步骤2: vi ...
- eclipse导入web工程变成Java工程,解决方案
经常在eclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目. 解决步骤: 1.进入项目目录,可看到.project文件,文本编辑器打开. 2.找到<nature ...