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的更多相关文章

  1. 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 ...

  2. 【转】Senior Data Structure · 浅谈线段树(Segment Tree)

    本文章转自洛谷 原作者: _皎月半洒花 一.简介线段树 ps: _此处以询问区间和为例.实际上线段树可以处理很多符合结合律的操作.(比如说加法,a[1]+a[2]+a[3]+a[4]=(a[1]+a[ ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  9. 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 ...

随机推荐

  1. 基于Android平台的简易人脸检测库

    代码地址如下:http://www.demodashi.com/demo/12135.html ViseFace 简易人脸检测库,不依赖三方库,可快速接入人脸检测功能. 项目依赖:compile 'c ...

  2. vs无法引用项目问题

    vs无法引用项目问题 2017年12月13日 14:45:31 阅读数:582 开发时编译报错--项目A未被引用,展开项目的引用,发现该项目实质已经被引用了,但是该引用上有个黄色三角感叹号,遂移除该引 ...

  3. 用Scratch2.0源码定制一个自己的编辑器

    用Scratch2.0源码定制一个自己的编辑器,换成自己的软件名称和图标,添加中文字体,修复汉化错误等等1.准备:下载Scratch2.0源码.安装开发工具Adobe Flash Builder4.7 ...

  4. sql server 集群配置

    Windows server2003 + sql server2005 集群配置安装 一:环境 软硬件环境 虚拟3台windows server 2003主机.当中一台做域控DC,另外两台作为节点wi ...

  5. Eval,Bind,<% %>,<%# %>和<%= %> 笔记

    1.<% %>用来绑定后台代码 如: < % for(int i=0;i<100;i++) { Reaponse.Write(i.ToString()); } %> 2. ...

  6. 详细的linux目录结构详细介绍

    详细的linux目录结构详细介绍 --树状目录结构图 下面红色字体为比较重要的目录 1./目录 目录 描述 / 第一层次结构的根,整个文件系统层次结构的根目录 /bin/ 需要在单用户模式可用的必要命 ...

  7. windows 和 linux 上 循环读取文件名称的区别和方法

    function showGetFileName($type){ $url="/opt/mobile_system/gscdn"; //另一台服务器映射到linux过来的路径. # ...

  8. Selenium+Python :WebDriver设计模式( Page Object )

    Page Object 设计原理 Page Object设计模式是Selenium自动化测试项目的最佳设计模式之一,强调测试.逻辑.数据和驱动相互分离. Page Object模式是Selenium中 ...

  9. C# 代码 手工 配置 Log4Net 2种方法

    这个初始化要在 获取 ILog 接口的代码之前完成, 之后按通常方式使用 log4net 就行了. 不用携带 config 配置文件. 方法1: /// <summary> /// 使用文 ...

  10. iOS 企业版 安装失败 原因

     首先要吐槽下国内的论坛水分略多,以下问题大多是查询stackoverflow等论坛解决的.推荐一款软件,Log Guru,用来查看app安装时的系统日志,很多问题要看日志才知道错误点. 1.首先有几 ...