二叉搜索树与双向链表
来源:互联网 发布:大学生兼职数据调查 编辑:程序博客网 时间:2024/06/05 02:14
二叉搜索树与双向链表
- 参与人数:2990
- 时间限制:1秒
- 空间限制:32768K
题目描述
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。
要求不能创建任何新的结点,只能调整树中结点指针的指向。
牛客网题目链接:点击这里
VS2010全部代码:
#include<iostream>#include<vector>using namespace std;struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};class Solution {public: void ChangeSubTree(TreeNode* SubRoot, TreeNode* &head, TreeNode* &tail) { //需要记录两个值,自下向上的,每一级的头和尾 //if(!SubRoot) return; if( !(SubRoot->left) && !(SubRoot->right) ) //只有根节点 { /*head=SubRoot->left; tail=SubRoot->right;*/ head=SubRoot; tail=SubRoot; return; } else { TreeNode* pl=SubRoot->left; TreeNode* pr=SubRoot->right; if( pl && !pr && !(pl->left) && !(pl->right)) //只有根和左叶子 { head=pl; pl->right=SubRoot; tail=SubRoot; return; } if( !pl && pr && !(pr->left) && !(pr->right)) //只有根和右叶子 { head=SubRoot; pr->left=SubRoot; tail=pr; return; } if( pl && pr && !(pr->left) && !(pr->right) && !(pl->left) && !(pl->right) ) //树和左右叶子 { head=pl; pl->right=SubRoot; pr->left=SubRoot; tail=pr; return; } } if(SubRoot->left) { TreeNode* H1=NULL; TreeNode* T1=NULL; //递归处理其他情况 ChangeSubTree( SubRoot->left, H1, T1); head=H1; T1->right=SubRoot; SubRoot->left=T1; } else head=SubRoot; if(SubRoot->right) { TreeNode* H2=NULL; TreeNode* T2=NULL; ChangeSubTree( SubRoot->right, H2, T2); tail=T2; H2->left=SubRoot; SubRoot->right=H2; } else tail=SubRoot; //调整后通过了[8,6,4,3] return; } TreeNode* Convert(TreeNode* pRootOfTree) { TreeNode* pHead=NULL; if(pRootOfTree) { TreeNode* pTail=NULL; ChangeSubTree(pRootOfTree, pHead, pTail); pTail->right=NULL; } return pHead; }};//建立二叉搜索树//[8,6,9,4,7]//[8,6,10,4,7,9,11]//[]//[8,6] 测试通过//[8,6,4]TreeNode* CreatBTSearch( vector<int> test){ TreeNode* root=NULL; if( !test.empty() ) { root=new TreeNode(test[0]); for(int i=1; i<test.size(); i++) { TreeNode* p=root; TreeNode* node=new TreeNode(test[i]); while(p) { if( test[i]>p->val && !(p->right) ) {p->right=node; break;} if( test[i]<p->val && !(p->left) ) {p->left=node; break;} if( test[i]>p->val && p->right ) p=p->right; if( test[i]<p->val && p->left ) p=p->left; } p=node; } } return root;}//输出二叉树:先序void PrintBT(TreeNode* root){ if(root) { cout<<root->val<<'-'; PrintBT(root->left); PrintBT(root->right); }}int main(){ Solution s1; vector<int> test1; TreeNode* result; test1.push_back(8); test1.push_back(6); //test1.push_back(10); test1.push_back(4); //test1.push_back(7); //test1.push_back(9); //test1.push_back(11); test1.push_back(3); cout<<"建立二叉搜索树…"<<endl; TreeNode* BS=NULL; BS=CreatBTSearch(test1); cout<<"完成建树!"<<endl; cout<<"先序输出搜索树"<<endl; PrintBT(BS); cout<<endl; cout<<"建立链表"<<endl; result=s1.Convert(BS); cout<<"链表输出:"<<endl; while(result) { cout<<result->val<<'-'; result=result->right; } cout<<endl; //const int a=1; //const int* b=&a; ////*b=a; //cout<<*b<<endl;}
说明:
1. 思想是用递归,递归传回两个参数。
牛客网通过图片:
0 0
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- caffe BasePrefetchingDataLayer 学习
- UVA1329 合作网络 解题报告
- iOS之重写frame和bounds的实用价值
- maven概念
- Linux minerd木马清除
- 二叉搜索树与双向链表
- 1006. 换个格式输出整数 (15)
- WineQQ
- vm8.0下安装centos6.8
- 2015ACM-ICPC 北京赛区 Problem I. Snake Carpet
- TCP的拥塞控制详解
- BZOJ3548: [ONTAK2010]Party
- 来源普索网 displaytag的export功能中文出现乱码...
- JAVA8开发指南第一章