## 二叉树（高度、结点个数）递归 原

老汉-憨憨

``````#include <iostream>
using namespace std;

// binary tree node
struct Node
{
int data;
Node *left,*right;
Node(int x)
{
data = x;
left = right = NULL;
}
};

int height(Node *root)
{
if (!root)
return 0;

int hLeft  = height(root->left);
int hRight = height(root->right);

return ((hLeft > hRight) ? (hLeft + 1) : (hRight + 1));
}

int size(Node *root)
{
if (!root)
return 0;

return (size(root->left) + size(root->right) + 1);
}

// Driver code
int main()
{
Node *root = new Node(1);
root->left = new Node(3);
root->left->left = new Node(2);
root->left->right = new Node(1);
root->left->right->left = new Node(1);
root->right = new Node(-1);
root->right->left = new Node(4);
root->right->left->left = new Node(1);
root->right->left->right = new Node(2);
root->right->right = new Node(5);
root->right->right->right = new Node(2);

int h = height(root);
cout << "height = " << h << endl;

int s = size(root);
cout << "size = " << s << endl;
return 0;
}
``````

``````height = 4
size = 11``````

### 老汉-憨憨

qq_38646470
2018/01/22
0
0

qq_38646470
2018/01/22
0
0

2017/12/19
0
2
[算法总结] 20 道题搞定 BAT 面试——二叉树

2018/09/04
0
0

2017/12/22
0
0

rabbitmq安装教程

RabbitMQ有Windows与Linux版本的，这里先写Windows版本的安装。 以前安装软件总是在百度上找某某安装教程，结果能按照教程安装好的软件真的不多。想起先前以为大牛说的一句话，去官网按照官网...

em_aaron

6
0
Android 贝塞尔曲线实践——波浪式运动

IamOkay

3
0
Nmap之防火墙/IDS逃逸

Frost729

2
0

java菜分享

7
0
Chocolatey 在Window搭建一个开发环境

8
0