惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
Last Week in AI
Last Week in AI
月光博客
月光博客
D
DataBreaches.Net
WordPress大学
WordPress大学
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
C
Check Point Blog
F
Fortinet All Blogs
B
Blog
小众软件
小众软件
Vercel News
Vercel News
罗磊的独立博客
有赞技术团队
有赞技术团队

博客园 - Zero Lee

调用栈(call stack) 关于STL allocator Calculate maximum sum of any subarray set Calcuate power n of x recursively 设计包含min函数的栈 类模板的模板友元函数定义 一道百度的面试题解答 非printf形式的十六进制和二进制打印(雅虎面试题) 一道腾讯面试题 (转)C++中extern “C”含义深层探索 selection algorithm to select nth small elements based on partition 删除与某个字符相邻且相同的字符 产生全排列的方法解析 一组数的全排列和组合程序实现 求一个正整数的平方根程序实现 [转]多线程队列的算法优化 [转载] STL allocator的介绍和一个基于malloc/free的allocator的简单实现 如何将一片内存链接成链表 One simple counted object pointer
Convert one binary search tree to double-linked list
Zero Lee · 2012-06-17 · via 博客园 - Zero Lee

Given one binary search tree, and convert it to one double-linked list
Cpp code demo as below:

 1 struct BSTreeNode
 2 {
 3     int m_value;
 4     BSTreeNode* m_pLeft;
 5     BSTreeNode* m_pRight;
 6 };
 7 
 8 BSTreeNode* convert(BSTreeNode* phead, bool asRight)
 9 {
10     if (!phead)
11         return NULL;
12     BSTreeNode* pLeft = 0, *pRight=0;
13     if (phead->m_pLeft)
14         pLeft = convert(phead->m_pLeft, false);
15     if (pLeft) {
16         pLeft->m_pRight = phead;
17         phead->m_pLeft = pLeft;
18     }
19     if (phead->m_pRight)
20         pRight = convert(phead->m_pRight, true);
21     if (pRight) {
22         pRight->m_pLeft = phead;
23         phead->m_pRight = pRight;
24     }
25     BSTreeNode* ptmp = phead;
26     if (asRight) {
27         while (ptmp->m_pLeft) {
28             ptmp = ptmp->m_pLeft;
29         }
30     } else {
31         while (ptmp->m_pRight) {
32             ptmp = ptmp->m_pRight;
33         }
34     }
35     return ptmp;
36 }
37 
38 BSTreeNode* convert2doublelist(BSTreeNode* phead)
39 {
40     return convert(phead, true);
41 }
42