打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
把二元查找树转变成排序的双向链表
#include<iostream>
using namespace std;
  
struct BSTreeNode {
    int m_nValue;
    BSTreeNode * m_pLeft;
    BSTreeNode * m_pRight;
    BSTreeNode(int value){
        m_nValue = value;
        m_pLeft = NULL;
        m_pRight = NULL;
    }
};
  
BSTreeNode * BSTreeNodeList[7];
  
BSTreeNode * buildTestTree() {
    int values [7] = {10,6,14,4,8,12,16};
    for(int i=0;i<sizeof(values)/sizeof(int);i++){
        BSTreeNodeList[i] = new BSTreeNode(values[i]);
    }
    BSTreeNode * root = BSTreeNodeList[0];
    root->m_pLeft = BSTreeNodeList[1];
    root->m_pRight = BSTreeNodeList[2];
  
    BSTreeNodeList[1]->m_pLeft = BSTreeNodeList[3];
    BSTreeNodeList[1]->m_pRight = BSTreeNodeList[4];
  
    BSTreeNodeList[2]->m_pLeft = BSTreeNodeList[5];
    BSTreeNodeList[2]->m_pRight = BSTreeNodeList[6];
  
    return root;
}
  
BSTreeNode * tail = NULL;
BSTreeNode * head = NULL;
  
void scan(BSTreeNode * root) {
    if (root == NULL) return ;
    scan(root->m_pLeft);
    if(tail == NULL) head = root;
    root->m_pLeft = tail;
    if (tail != NULL) tail->m_pRight = root;
    tail = root;
    scan(root->m_pRight);
}
  
int main(){
    BSTreeNode * root = buildTestTree();
    scan(root);
    BSTreeNode * p = head;
    while(p!=NULL){
        cout<<p->m_nValue<<endl;
        p = p->m_pRight;
    }
    cout<<"------------"<<endl;
    p = tail;
    while(p!=NULL){
        cout<<p->m_nValue<<endl;
        p = p->m_pLeft;
    }
    return 0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
算法13(把二元查找树转变成排序的双向链表 )
[答案V0.1版]精选微软等数据结构+算法面试100题 [前20题] - 结构之法 算法之...
程序员面试题精选(01)-把二元查找树转变成排序的双向链表
二叉树的前序、中序、后序遍历(非递归)
二叉树相关算法总结
在二叉树中找出和为某一值的所有路径
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服