求二叉树的高度和销毁一颗二叉树——题集九

本文介绍了如何求解二叉树的高度和销毁二叉树的算法,同时探讨了链表翻转问题,这是一个链表逆置的升级变型。提供了相关源代码和运行示例。

求二叉树的高度和销毁一颗二叉树——题集九

       今天来分享一下,求二叉树的高度销毁一颗二叉树以及如何实现链表翻转(链表逆置的升级变型

       求二叉树的高度销毁一颗二叉树(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;
}

运行结果:

 

      分享如上,如有错误,望斧正!愿大家学得开心,共同进步!

### 使用递归算法创建二叉树高度 #### 创建二叉树 通过递归方式构建二叉树时,通常采用前序遍历的方式输入节点值。如果遇到特定字符(如`#`),则表示为空节点。以下是基于C语言的递归创建二叉树的方法: ```c #include<stdio.h> #include<stdlib.h> typedef struct TreeNode { char data; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 递归创建二叉树 TreeNode* createTree() { char ch; scanf(" %c", &ch); if (ch == '#') { return NULL; // 遇到'#'返回空指针 } TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->data = ch; node->left = createTree(); // 递归创建左子树 node->right = createTree(); // 递归创建右子树 return node; } ``` 此代码片段实现了通过用户输入来递归创建一棵二叉树的功能。 --- #### 计算二叉树高度 利用递归方法计算二叉树高度的核心思想是:对于任意一个节点,它的高度为其左右子树高度的最大值加1。当到达叶子节点时,返回0作为基础条件[^1]。 下面是完整的递归函数用于计算二叉树高度: ```c int treeHeight(TreeNode* root) { if (root == NULL) { // 如果当前节点为空,则返回高度为0 return 0; } int leftHeight = treeHeight(root->left); // 左子树高度 int rightHeight = treeHeight(root->right); // 右子树高度 return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1; // 返回较大者加1 } ``` 以上代码展示了如何通过递归调用分别获取左右子树的高度,并最终得出整个二叉树高度。 --- #### 完整程序示例 下面是一个完整的程序实例,它包含了二叉树的创建以及高度的计算过程: ```c #include<stdio.h> #include<stdlib.h> typedef struct TreeNode { char data; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 递归创建二叉树 TreeNode* createTree() { char ch; scanf(" %c", &ch); if (ch == '#') { return NULL; } TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->data = ch; node->left = createTree(); node->right = createTree(); return node; } // 计算二叉树高度 int treeHeight(TreeNode* root) { if (root == NULL) { return 0; } int leftHeight = treeHeight(root->left); int rightHeight = treeHeight(root->right); return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1; } // 主函数测试 int main() { printf("请输入二叉树的前序序列(使用 '#' 表示空节点):\n"); TreeNode* root = createTree(); int height = treeHeight(root); printf("二叉树高度为:%d\n", height); return 0; } ``` 上述代码提供了一个交互式的环境让用户输入二叉树的前序序列,并自动计算出该二叉树高度。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值