C語言實(shí)現(xiàn)計(jì)算樹的深度的方法
更新時(shí)間:2014年09月16日 16:06:18 投稿:shichen2014
這篇文章主要介紹了C語言實(shí)現(xiàn)計(jì)算樹的深度的方法,針對數(shù)據(jù)結(jié)構(gòu)中樹進(jìn)行操作的方法,在算法設(shè)計(jì)中比較常見,需要的朋友可以參考下
本文實(shí)例講述了C語言實(shí)現(xiàn)計(jì)算樹的深度的方法。是算法設(shè)計(jì)中常用的技巧。分享給大家供大家參考。具體方法如下:
/*
* Copyright (c) 2011 alexingcool. All Rights Reserved.
*/
#include <iostream>
using namespace std;
struct Node {
Node(int i = 0, Node *l = NULL, Node *r = NULL) : data(i), left(l), right(r) {}
int data;
Node *left;
Node *right;
};
Node* Construct() {
Node *node4 = new Node(7, NULL, new Node(3));
Node *node3 = new Node(4);
Node *node2 = new Node(12);
Node *node1 = new Node(5, node3, node4);
Node *root = new Node(10, node1, node2);
return root;
}
int GetTreeHeight(Node *root) {
if(root == NULL)
return 0;
return max(GetTreeHeight(root->left) + 1, GetTreeHeight(root->right) + 1);
}
void main() {
Node *root = Construct();
int height = GetTreeHeight(root);
cout << "tree height is: " << height << endl;
}
希望本文所述實(shí)例對大家C程序算法設(shè)計(jì)的學(xué)習(xí)有所幫助。
相關(guān)文章
C語言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
c++報(bào)錯(cuò)問題解決方案lvalue required as left opera
這篇文章主要介紹了c++報(bào)錯(cuò):lvalue required as left operand of assignment,出現(xiàn)此錯(cuò)誤原因,是因?yàn)?,等號左邊是不可被修改的表達(dá)式或常量,而表達(dá)式或常量不能作為左值,需要的朋友可以參考下2023-01-01
詳解C++數(shù)組和數(shù)組名問題(指針、解引用)
這篇文章主要介紹了詳解C++數(shù)組和數(shù)組名問題(指針、解引用),指針的實(shí)質(zhì)就是個(gè)變量,它跟普通變量沒有任何本質(zhì)區(qū)別,指針本身是一個(gè)對象,同時(shí)指針無需在定義的時(shí)候賦值,具體內(nèi)容詳情跟隨小編一起看看吧2021-09-09

