求二叉樹的高度和銷燬一顆二叉樹——題集九

求二叉樹的高度和銷燬一顆二叉樹——題集九

       今天來分享一下,求二叉樹的高度銷燬一顆二叉樹以及如何實現鏈表翻轉(鏈表逆置的升級變型

       求二叉樹的高度銷燬一顆二叉樹(Destroy(Node* root) )的源代碼和運行示例。

源代碼如下:

#include<iostream>
using namespace std;
 
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x)
:val(x)
,left(NULL)
,right(NULL)
{
    }
};
 
 
int _Heigt(TreeNode* root){
if(root==NULL) return 0;
 
int left = _Heigt(root->left);
int right = _Heigt(root->right);
 
return left>right?left+1:right+1;
}
 
//二叉樹的高度
int Height(TreeNode* root){//二叉樹的高度
if(root == NULL)
return 0;
 
return _Heigt(root);//遞歸簡單
}
 
void _Destroy(TreeNode*  & root){
if(root==NULL) return;
 
_Destroy(root->left);
_Destroy(root->right);
 
delete root;
root=root->left;
root=NULL;
 
return ;
}
 
//銷燬一顆二叉樹
void Destroy(TreeNode*& root){
if(root == NULL)
return;
 
_Destroy(root);
return ;
}
 
void TestTree(){//二叉樹的高度+銷燬一顆二叉樹
TreeNode * pRoot1=new TreeNode(1);
TreeNode * pRoot2=new TreeNode(2);
TreeNode * pRoot3=new TreeNode(3);
TreeNode * pRoot4=new TreeNode(4);
TreeNode * pRoot5=new TreeNode(5);
TreeNode * pRoot6=new TreeNode(6);
TreeNode * pRoot7=new TreeNode(7);
TreeNode * pRoot8=new TreeNode(8);
 
pRoot1->left = pRoot2;
pRoot1->right = pRoot3;
pRoot2->left = pRoot4;
pRoot2->right = pRoot5;
pRoot3->left = pRoot6;
pRoot3->right = pRoot7;
pRoot4->left = pRoot8;
 
cout<<"求二叉樹的高度"<<endl;
cout<<"Height(pRoot1): "<<Height(pRoot1)<<endl;//二叉樹的高度
cout<<"Height(pRoot2): "<<Height(pRoot2)<<endl;
cout<<"Height(pRoot8): "<<Height(pRoot8)<<endl;
cout<<endl;
 
cout<<"銷燬一顆二叉樹"<<endl;
cout<<"Height(pRoot1): "<<Height(pRoot1)<<endl;
Destroy(pRoot1);//銷燬一顆二叉樹
cout<<"Destroy(pRoot1) -> Height(pRoot1): "<<Height(pRoot1)<<endl;
cout<<endl;
}
 
int main(){
TestTree();//二叉樹的高度+銷燬一顆二叉樹
system("pause");
return 0;
}

運行結果:

 

測試用例的二叉樹是長成這樣的哦。


       鏈表翻轉的源代碼和運行示例。

       給出一個鏈表和一個數k,比如鏈表1→2→3→4→5→6,k=2,翻轉後2→1→4→3→6→5,若k=3,翻轉後3→2→1→6→5→4,若k=4,翻轉後4→3→2→1→5→6,用程序實現Node* RotateList(Node* list, size_t k). 提示:這個題是鏈表逆置的升級變型

源代碼如下:

#include<iostream>
using namespace std;
 
struct ListNode{
int val;
ListNode* next;
ListNode(int _val)
:val(_val)
,next(NULL)
{}
};
 
void _RotateList(ListNode*& begin, ListNode*& end){//begin和end 不可能爲NULL
ListNode* cur = begin;
ListNode* next = cur->next;
 
while(next != end){
ListNode* tmp=next->next;
 
next->next = cur;
cur=next;
next=tmp;
 
}
next->next = cur;
}
ListNode* RotateList(ListNode* list, size_t k){//遞歸
ListNode* head=list;
if(k<=1) return head;
 
ListNode* begin=list;
ListNode* end=list;
 
 
ListNode* prev=NULL;
ListNode* next=NULL;
 
while(end != NULL){
size_t i=1;
 
while(i<k && end->next != NULL){
++i;
end = end->next;
}
 
if(end->next != NULL){
next=end->next;
_RotateList(begin, end);
if(prev == NULL){
head=end;
}
else{
prev->next=end;
}
 
begin->next =next;
prev=begin;
begin=end=next;
i=1;
}
else{
end=end->next;
}
}
 
return head;
}
 
void Printf(ListNode* l1){//打印
int i=0;
while(l1!=NULL){
if(i!=0)
cout<<"->";
cout<<l1->val;
l1=l1->next;
++i;
}
printf("\n");
}
 
void TestRotateList(){////如何實現鏈表翻轉
ListNode l1(1);
ListNode l2(2);
ListNode l3(3);
ListNode l4(4);
ListNode l5(5);
l1.next = &l2;
l2.next = &l3;
l3.next = &l4;
l4.next = &l5;
 
cout<<"原鏈表遍歷打印:"<<endl;
Printf(&l1);
ListNode* lA=RotateList(&l1, 1);//遞歸
cout<<"1次翻轉打印->RotateList(&l1, 1):";
Printf(lA);
 
cout<<endl<<"原鏈表遍歷打印:"<<endl;
Printf(&l1);
lA=RotateList(&l1, 2);//遞歸
cout<<"2次翻轉打印->RotateList(&l1, 2):";
Printf(lA);
 
l1.next = &l2;
l2.next = &l3;
l3.next = &l4;
l4.next = &l5;
 
cout<<endl<<"原鏈表遍歷打印:"<<endl;
Printf(&l1);
lA=RotateList(&l1, 3);//遞歸
cout<<"3次翻轉打印->RotateList(&l1, 3):";
Printf(lA);
cout<<endl;
}
 
int main(){
TestRotateList();////如何實現鏈表翻轉
system("pause");
return 0;
}

運行結果:

 

      分享如上,如有錯誤,望斧正!願大家學得開心,共同進步!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章