博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
求二叉树中结点的最大值(所有结点的值都是正整数)
阅读量:4140 次
发布时间:2019-05-25

本文共 1143 字,大约阅读时间需要 3 分钟。

#include
#define N 63using namespace std;char str[] = "ab#d##a#e##";int i = -1;typedef struct node{ struct node *leftChild; struct node *rightChild; char data;}BiTreeNode, *BiTree;//生成一个结点BiTreeNode *createNode(int i){ BiTreeNode * q = new BiTreeNode; q->leftChild = NULL; q->rightChild = NULL; q->data = i; return q;}BiTree createBiTree1(){ BiTreeNode *p[N] = {NULL}; int i; for(i = 0; i < N; i++) p[i] = createNode(i + 1); // 把结点连接成树 for(i = 0; i < N/2; i++) { p[i]->leftChild = p[i * 2 + 1]; p[i]->rightChild = p[i * 2 + 2]; } return p[0];}void createBiTree2(BiTree &T){ i++; char c; if(str[i] && '#' == (c = str[i])) T = NULL; else { T = new BiTreeNode; T->data = c; createBiTree2(T->leftChild); createBiTree2(T->rightChild); }}int getBiggestValue(BiTree T){ if(NULL == T) return 0; int maxLeft = getBiggestValue(T->leftChild); int maxRight = getBiggestValue(T->rightChild); int max = maxLeft > maxRight ? maxLeft : maxRight; return max > T->data ? max : T->data;}int main(){ BiTree T1; T1 = createBiTree1(); cout << getBiggestValue(T1) << endl; BiTree T2; createBiTree2(T2); cout << getBiggestValue(T2) << endl; return 0;}

 

转载地址:http://gdzti.baihongyu.com/

你可能感兴趣的文章
不同场景下 MySQL 的迁移方案
查看>>
解决zabbix图中出现中文乱码问题
查看>>
zabbix监控mysql客户端
查看>>
windows文件上传到linux系统的文件内容问题
查看>>
hadoop安装之-sqoop
查看>>
numpy向量加一个常数=向量中的每个值加上这个常数,最后返回一个同维的向量
查看>>
从RNN一步步通俗易懂T理解LSTM
查看>>
多文多面阐述HMM很清晰
查看>>
机器学习算法推导的较好例子
查看>>
loss函数为何选交叉熵
查看>>
院士 人工智能专业理解
查看>>
AttributeError: module 'tensorflow' has no attribute 'random_normal'
查看>>
谷歌视频播放速度调节插件安装方法
查看>>
怎么自学《现代控制理论》
查看>>
np.array 与np.asarray区别
查看>>
understanding LSTM networks(zhuan)
查看>>
SVM讲的成体系的,自圆其说较好的一篇,上午没读完,KKT第二部分不等式有疑问没弄懂,复制后继续读完
查看>>
提升算法的sklearn-kit的API
查看>>
什么时候使用临时表?
查看>>
basicLSTMCELL() num_units参数代表了LSTM输出向量的维数
查看>>