文章目录人生如逆旅,我亦是行人
- 题目描述
- 迭代
- 递归
本来还有一个最大深度的 ,但是我想 你看这么多了,肯定是问题 不大,咱们来试一下最小深度
题目描述给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
输入:root = [3,9,20,null,null,15,7]
输出:2
思路:这里依然是使用层序遍历,前面已经创建过一个树了 我们只需要在遍历的过程中 判断此时的结点是否是叶子结点即可
若是叶子结点直接返回,因为后面的叶子结点的深度不会比第一个遍历的叶子结点的深度小
直接给出代码
#include#define ElemType int using namespace std; typedef struct BiNTree{ struct BiNTree* Lchild; ElemType data; struct BiNTree* Rchild; struct BiNTree* Next; }BiNTree; void Creat(BiNTree* &T){ int val; BiNTree* cur;//定义一个cur queue Q; queue TreeQue; cout<<"请输入层序遍历的序列"< cout<<"树为空"< T=(BiNTree*)malloc(sizeof(BiNTree)); T->data=Q.front(); TreeQue.push(T); } Q.pop(); while(!Q.empty()){//我们每一次处理 处理的都是左右孩子,所以也就需要两个if cur=TreeQue.front();//取出此时树队中的第一元素 if(Q.front()==-1){//此时这个结点值为NULL cout< data<<"左孩子为空"< Lchild=NULL; } else{//新生成一个树结点用来存放此时的结点信息 cout< data<<"左孩子为"< Lchild=(BiNTree*)malloc(sizeof(BiNTree)) ; cur->Lchild->data=Q.front(); TreeQue.push(cur->Lchild); } Q.pop();//处理完数据之后需要弹出 来取此结点右子树的值 //上面的if else 是处理结点左孩子 下面我们来处理右孩子 if(Q.front()==-1){ cout< data<<"右孩子为空"< Rchild=NULL; } else{ cout< data<<"右孩子为"< Rchild=(BiNTree*)malloc(sizeof(BiNTree)); cur->Rchild->data=Q.front(); TreeQue.push(cur->Rchild); } Q.pop(); TreeQue.pop(); } } void LevelOrder(BiNTree* T){//这里我们是使用的是队列来完成这个操作 queue Q; BiNTree* cur;int deep; if(T==NULL){//跟前面一样 若是为空则不需要进队 直接返回即可 return ; } Q.push(T); while(!Q.empty()){ deep+=1; int max=Q.front()->data;//别忘了使用这一行的第一个值来进行初始化 int size=Q.size();//需要一个值来保存 因为Q.size 是一直变得 for(int i=0;i cur=Q.front();Q.pop(); if(cur->Lchild==NULL&&cur->Rchild==NULL){ cout<<"此时叶子结点的最小深度是"< Lchild) Q.push(cur->Lchild); if(cur->Rchild) Q.push(cur->Rchild); } } } main(){ int i=0; BiNTree* T=NULL; vector > result; Creat(T); LevelOrder(T); }
前面使用过层序遍历了 也比较简单 那么这道题可否使用其他的遍历方式?若是使用递归也就是找到此根结点的左右子树的较小的一个 返回值的时候别忘了加一就行,那么可以使用前中后那种方式?这里需要知道左右子树之后再进行判断操作此根节点 所以这里认为后序是比较好的 下面写出代码
递归#include#define ElemType int using namespace std; typedef struct BiNTree{ struct BiNTree* Lchild; ElemType data; struct BiNTree* Rchild; }BiNTree; void Creat(BiNTree* &T){ int val; BiNTree* cur;//定义一个cur queue Q; queue TreeQue; cout<<"请输入层序遍历的序列"< cout<<"树为空"< T=(BiNTree*)malloc(sizeof(BiNTree)); T->data=Q.front(); TreeQue.push(T); } Q.pop(); while(!Q.empty()){//我们每一次处理 处理的都是左右孩子,所以也就需要两个if cur=TreeQue.front();//取出此时树队中的第一元素 if(Q.front()==-1){//此时这个结点值为NULL cout< data<<"左孩子为空"< Lchild=NULL; } else{//新生成一个树结点用来存放此时的结点信息 cout< data<<"左孩子为"< Lchild=(BiNTree*)malloc(sizeof(BiNTree)) ; cur->Lchild->data=Q.front(); TreeQue.push(cur->Lchild); } Q.pop();//处理完数据之后需要弹出 来取此结点右子树的值 //上面的if else 是处理结点左孩子 下面我们来处理右孩子 if(Q.front()==-1){ cout< data<<"右孩子为空"< Rchild=NULL; } else{ cout< data<<"右孩子为"< Rchild=(BiNTree*)malloc(sizeof(BiNTree)); cur->Rchild->data=Q.front(); TreeQue.push(cur->Rchild); } Q.pop(); TreeQue.pop(); } } int PostOrder(BiNTree* T){ if(T==NULL){ return 0;//假如就是一个空树 ,当然此时的树深就是0 } return PostOrder(T->Lchild) Rchild)? PostOrder(T->Lchild)+1:PostOrder(T->Rchild)+1; } main(){ BiNTree* T=NULL; Creat(T); cout<<"此时的树的最小高度是"; cout<



