binary_tree_max_height
binary_tree_max_height.cpp 源码
// 二叉树的最大深度
#include "vector"
#include "queue"
#include "algorithm"
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() {}
TreeNode(int val) : val(val) {}
TreeNode(int val, TreeNode *left, TreeNode *right) : val(val), left(left), right(right) {}
};
class Solution {
public:
// 递归
int maxDepth(TreeNode* root) {
if (!root) {
return 0;
}
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
// 广度优先搜索
int maxDepth(TreeNode* root) {
if (!root) {
return 0;
}
queue<TreeNode*> que;
int height = 0;
que.push(root);
while (!que.empty()) {
int size = que.size();
while (size-- > 0) {
TreeNode* node = que.front();
que.pop();
if (node->left) {
que.push(node->left);
}
if (node->right) {
que.push(node->right);
}
}
height++;
}
return height;
}
};
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦