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 ...
随机推荐
- HTML <!DOCTYPE> (转自w3school)
定义和用法 <!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. <!DOCTYPE> 声明不是 HTML 标签:它是指示 we ...
- 如何修改myeclipse中web项目的工作路径或默认路径
如何修改myeclipse中web项目的工作路径或默认路径 博客分类: J2EE开发技术指南 安装好myeclipse后,第一次启动myeclipse时,都会弹出会弹出Workspace Laun ...
- automaticallyAdjustsScrollViewInsets(UITextView文字顶部留有空白)
iOS7新添加的UIViewController的属性automaticallyAdjustsScrollViewInsets 此属性默认为YES,这样UIViewController下如果只有一个U ...
- 使用xib定义的UITableViewCell的复用identifier
使用xib自定义cell的时候,需要在xib中指定复用identifier(通常与类名一致即可),在编码的时候,也应该使用该identifier而不应该自定义其他identifier,否则,可能导致程 ...
- Android中ListView分类
1 http://my.oschina.net/bv10000/blog/106436 2
- VS2012 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService 未找到与约束ContractName,无法打开项目的解决方案 SQLyog 注册码
VS2012 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService 最近新换了系统还真是问题多多呀! ...
- vi/vim复制粘贴命
1. 选定文本块.使用v进入可视模式,移动光标键选定内容. 2.复制的命令是y,即yank(提起) ,常用的命令如下: y 在使用v模式选定了某一块的时候,复制选定块到缓冲区用: ...
- java游戏开发之基础
© 版权声明:本文为博主原创文章,转载请注明出处 游戏图形界面开发基础 AWT:(Abstract Window Toolkit,抽象窗口工具集) AWT中包含图形界面编程的基本类库,是Java语言G ...
- 模拟和数字低通滤波器的MATLAB实现
低通滤波器参数:Fs=8000,fp=2500,fs=3500,Rp=1dB,As=30dB,其他滤波器可以通过与低通之间的映射关系实现. %%模拟滤波器 %巴特沃斯——滤波器设计 wp=2*pi*2 ...
- 使用wifi连接eclipse进行android程序调试
首先手机必须是root过的.能够使用百度一键root工具. 然后,在手机中打开这个终端(terminal)应用,输入例如以下命令: su setprop service ...