这棵树有多高
运行下面这段程序:
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
struct TNode {
string name;
TNode* left = nullptr;
TNode* right = nullptr;
};
TNode* build() {
TNode* root = new TNode{"总部"};
root->left = new TNode{"华北"};
root->right = new TNode{"华南"};
root->left->left = new TNode{"北京"};
root->left->right = new TNode{"天津"};
root->right->right = new TNode{"广州"};
return root;
}
int height(TNode* node) {
if (node == nullptr) return 0;
int a = height(node->left);
int b = height(node->right);
return 1 + (a > b ? a : b);
}
int main() {
cout << height(build()) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论