题目:编写递归算法,求二叉树中元素值为x的结点的深度
题目:编写递归算法,求二叉树中以元素值为x的结点为根的子树的深度
//题目:编写递归算法,求二叉树中元素值为x的结点的深度
//对应代码的GetDepth_OneValue函数
//题目:编写递归算法,求二叉树中以元素值为x的结点为根的子树的深度
//对应代码的GetTreeDepth_OneValueAsRoot函数
#include<iostream>
using namespace std;
#define TElemType double
typedef struct BiTNode
{
TElemType data;
struct BiTNode* lchild,* rchild;
}BiTNode,* BiTree;
void Creat_BiTree(BiTree& T)//创建二叉树
{
TElemType num;
if(cin>>num){
T=new BiTNode;
if(!T) exit(-2);
T->data=num;
Creat_BiTree(T->lchild);
Creat_BiTree(T->rchild);
}
if(!cin){
T=nullptr;
cin.clear();
cin.sync();
}
}
void GetDepth_OneValue(BiTree T,TElemType& e,int* depth)//先序递归求得e的深度*depth
//每进入一个递归函数,*depth就+1;
//每从一个递归函数出来,*depth就-1
{
(*depth)++;//每进入一个递归函数,*depth++
if(T){