您的位置:首页 > 其它

2017.11.5 LeetCode - 104. Maximum Depth of Binary Tree 【dfs的应用】

2017-11-06 18:32 323 查看

104. Maximum Depth of Binary Tree

Description

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

题意: 求一颗二叉树的深度

分析: 直接dfs模拟即可

参考函数

class Solution {
public:
int res = 0;

void dfs(TreeNode* root,int c) {
if(!root) return ;
res = max(res,c);
dfs(root->left,c+1);
dfs(root->right,c+1);
}
int maxDepth(TreeNode* root) {
dfs(root,1);
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: