算法面试100题——1、把二元查找树转变成排序的双向链表
来源:互联网 发布:王宝强剧情反转知乎 编辑:程序博客网 时间:2024/05/20 23:59
题目:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
10
/ \
6 14
/ \ / \
4 8 12 16
转换成双向链表
4=6=8=10=12=14=16。
首先我们定义的二元查找树 节点的数据结构如下:
struct BSTreeNode
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};
/*用途:**说明:**算法:*///#define LOCAL#include <cstdio>#include <cstdlib>#include <stack>using namespace std;struct BSTreeNode{ int m_nValue; BSTreeNode *m_pLeft; BSTreeNode *m_pRight;};typedef BSTreeNode* BST;BSTreeNode* NewNode();BSTreeNode* NewNode(int v);//递归实现BSTreeNode* ToDLinkList(BST bst);void Traverse(BST bst, BSTreeNode* &pre);//显示栈实现 BSTreeNode* ToDLinkList2(BST bst); void TestHead(BSTreeNode* head);void TestTail(BSTreeNode* tail);int main(){#ifdef LOCAL freopen(".in", "r", stdin); freopen(".out", "w", stdout);#endif BST bst = NewNode(); BSTreeNode *n0 = NewNode(10); bst->m_pLeft = n0; BSTreeNode *n1 = NewNode(6); n0->m_pLeft = n1; BSTreeNode *n2 = NewNode(14); n0->m_pRight = n2; BSTreeNode *n3 = NewNode(4); n1->m_pLeft = n3; BSTreeNode *n4 = NewNode(8); n1->m_pRight = n4; BSTreeNode *n5 = NewNode(12); n2->m_pLeft = n5; BSTreeNode *n6 = NewNode(16); n2->m_pRight = n6; BSTreeNode* tail = bst->m_pLeft; //测试尾指针 while(tail->m_pRight) tail = tail->m_pRight; BSTreeNode* head;// head = ToDLinkList(bst); //递归实现 head = ToDLinkList2(bst); //显示栈实现 TestHead(head); printf("\n"); TestTail(tail); return 0;}BSTreeNode* NewNode(){ BSTreeNode* pNode = (BSTreeNode*)malloc(sizeof(BSTreeNode)); pNode->m_pLeft = pNode->m_pRight = NULL; return pNode;}BSTreeNode* NewNode(int v){ BSTreeNode* pNode = (BSTreeNode*)malloc(sizeof(BSTreeNode)); pNode->m_nValue = v; pNode->m_pLeft = pNode->m_pRight = NULL; return pNode;}//递归实现BSTreeNode* ToDLinkList(BST bst){ BSTreeNode* head = bst; while(head->m_pLeft) head = head->m_pLeft; BSTreeNode* pre = NULL; //当前节点的前一个指针 Traverse(bst->m_pLeft, pre); return head;}void Traverse(BST bst, BSTreeNode* &pre){ if(bst != NULL){ Traverse(bst->m_pLeft, pre); bst->m_pLeft = pre; if(pre) pre->m_pRight = bst; pre = bst; Traverse(bst->m_pRight, pre); }}//显示栈实现BSTreeNode* ToDLinkList2(BST bst){ stack<BSTreeNode*> s; BSTreeNode* head = bst; while(head->m_pLeft) head = head->m_pLeft; BSTreeNode* pre = NULL; BSTreeNode* p = bst->m_pLeft; while(!s.empty() || p){ if(p){ s.push(p); p = p->m_pLeft; } else{ BSTreeNode* m_pTop = s.top(); s.pop(); m_pTop->m_pLeft = pre; if(pre) pre->m_pRight = m_pTop; pre = m_pTop; if(m_pTop->m_pRight) p = m_pTop->m_pRight; } } return head;} void TestHead(BSTreeNode* head){ while(head){ printf("%d ", head->m_nValue); head = head->m_pRight; }}void TestTail(BSTreeNode* tail){ while(tail){ printf("%d ", tail->m_nValue); tail = tail->m_pLeft; }}
阅读全文
0 0
- 算法面试100题——1、把二元查找树转变成排序的双向链表
- 面试100题:1 把二元查找树转变成排序的双向链表
- 微软面试100题 第一题 把二元查找树转变成排序的双向链表
- 面试100题:1.把二元查找树转变成排序的双向链表
- 程序员面试100题---1.把二元查找树转变成排序的双向链表
- 面试100题:1.把二元查找树转变成排序的双向链表
- 微软面试(1/100)---把二元查找树转变成排序的双向链表
- 微软等数据结构+算法面试100题(43)-- 把二元查找树转变成排序的双向链表
- 把二元查找树转变成排序的双向链表——精选微软经典的算法面试100题中第一题
- 每日一道算法题1 ——把二元查找树转变成排序的双向链表
- [面试中的算法]把二元查找树转变成排序的双向链表
- 面试算法001-- 把二元查找树转变成排序的双向链表
- 每天一道算法题(8)——把二元查找树转变成排序的双向链表
- 算法与数据结构面试题(1)-把二元查找树转变成排序的双向链表
- 微软面试100之1 把二元查找树转变成排序的双向链表(树)
- 把二元查找树转变成排序的双向链表
- 把二元查找树转变成排序的双向链表
- 把二元查找树转变成排序的双向链表
- 浏览器的两种模式quirks mode 和strict mode
- shell工具之awk
- Mysql常见错误码讲解
- try--finally中,try里面的return和finaly执行的先后
- [Leetcode] 386. Lexicographical Numbers 解题报告
- 算法面试100题——1、把二元查找树转变成排序的双向链表
- python 的字符串格式判断
- TAL好未来秋招编程题
- 深入Java单例模式
- PHP如何提高性能(分析)
- js中的getAttribute()
- LoadRunner脚本编写
- spring boot war 打包
- Android Jpush 6009错误代码解决方案