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 ...
随机推荐
- mybatis 遇到的问题
顺序问题:在resultmap中,result必须在association之前.否则会报错 只查出一条记录的问题 :重名的id,要起别名.而且不能忽略. mybaits错误解决:There is n ...
- [JAVA · 0基础]:11.终结者-final
关于finalkeyword的总结,是非常easy学习和掌握的,大致分为用final修饰的符号常量.类.方法.属性四部分.而这四部分的内容也是我们必须掌握的,非常有用.在项目中常常来利用finalke ...
- ArcObject IFeature set_Shape()和Delete()报错
这样的问题主要是Ifeature实际在数据库里面不存在!可是通过IFeatureClass.getFeature()又可以得到! 详细操作流程: 首先是对要素进行删除,可是通过IFeatureClas ...
- linux下的3种DDOS软件介绍
什么是TFN2K? TFN2K 的作者是著名的德国黑客mixter. http://mixter.void.ru/papers.html TFN2K通过主控端利用大量代理端主机的资源进行对一个或多个目 ...
- python在windows系统中打印中文乱码
转自:http://www.111cn.net/phper/python/58920.htm 中文乱码对于程序开发人员来讲不是什么怪事情了,今天我在使用python打印中文时就出现乱码了,下面我们一起 ...
- Java设计模式透析之 —— 策略(Strategy)
今天你的leader兴致冲冲地找到你,希望你能够帮他一个小忙.他如今急着要去开会.要帮什么忙呢?你非常好奇. 他对你说.当前你们项目的数据库中有一张用户信息表.里面存放了非常用户的数据.如今须要完毕一 ...
- java.io.IOException: Illegal partition for 67 (-1)
今天写MapReduce的分区进行排序的功能,自己写了一个Partitioner,然后用的时候就错了 public static class MyPartition extends Partition ...
- 【JMeter4.0学习(五)】JMeter对服务器监控测试脚本开发
目录: 下载相关JMeter插件 服务器监控测试脚本开发 附:参考相关文档 本文主要来说一下如何通过JMeter插件来监控服务器CPU.内存.磁盘.网络等相关资源. 一.首先,需要下载相关JMeter ...
- 【React Native开发】React Native配置执行官方样例-刚開始学习的人的福音(8)
),React Native技术交流4群(458982758),请不要反复加群! 欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...
- 看了就很快学会jQuery
一.jQuery简介与第一个jQuery程序 1.1.jQuery简介 1.2.jQuery特点 1.3.jQuery版本 1.4.获得jQuery库 1.5.第一个jQuery程序 二.jQuery ...