栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > C++面试题库

C/C++面试题 – 把二元查找树变成排序的双向链表

C/C++面试题 – 把二元查找树变成排序的双向链表

#include "stdafx.h"struct node{node * left;node * right;int value;};void convert(node * root, node *& last){if(root == NULL)return;if(root->left)convert(root->left, last);root->left = last;if(last)last->right = root;last = root;if(root->right)convert(root->right, last);}node * solution(node * root){node * last = NULL;convert(root, last);while(last && last->left)last = last->left;return last;}int _tmain(int argc, _TCHAR* argv[]){node * n1 = new node();node * n2 = new node();node * n3 = new node();node * n4 = new node();n1->left = n2;n1->right = n3;n1->value = 3;n2->left = n4;n2->right = NULL;n2->value = 2;n3->left = NULL;n3->right = NULL;n3->value = 4;n4->left = NULL;n4->right = NULL;n4->value = 1;node * n = solution(n1);while(n){printf("%d ",n->value);n = n->right;}return 0;}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/265253.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号