1.二叉树的最大深度
(1)递归
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root)return 0;
int m = maxDepth(root->left);
int n = maxDepth(root->right);
if (m > n)return m + 1;
else return n + 1;
}
};
(2)广度优先搜索
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root)return 0;
queue<TreeNode*>table;
table.push(root);
int ans = 0;
while (!table.empty())
{
int sz = table.size();
while (sz > 0)
{
TreeNode*node = table.front();
table.pop();
if(node->left)
table.push(node->left);
if(node->right)
table.push(node->right);
sz--;
}
a