tree.cpp
来自「数据结构中经典的树结构的C++语言描述算法」· C++ 代码 · 共 78 行
CPP
78 行
// Tree.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
using namespace std;
typedef struct TreeNode
{
TreeNode * L;
TreeNode * R;
int data;
}TreeNode,*BT;
void IniTree(BT &T)
{
int d;
cin>>d;
if(d==0)
T=NULL;
else
{
T=(TreeNode *)malloc(sizeof(TreeNode));
T->data=d;
IniTree(T->L);
IniTree(T->R);
}
}
void Preorder(TreeNode *T)
{
if(T)
{
cout<<T->data;
Preorder(T->L);
Preorder(T->R);
}
}
void Depth(TreeNode *T,int h,int & depth)
{
if(T)
{
if(h>depth)
depth=h;
Depth(T->L,h+1,depth);
Depth(T->R,h+1,depth);
}
}
int main()
{
BT T;
IniTree(T);
//IniTree(T);
Preorder(T);
int h=1;
int depth=0;
Depth(T,h,depth);
cout<<depth;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?