AcWing 71. 二叉树的深度
原题链接
简单
作者:
chipizz
,
2019-02-23 13:25:55
,
所有人可见
,
阅读 3331
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void dfs(TreeNode* root, int d) {
if (root->left) dfs(root->left, d+1);
if (root->right) dfs(root->right, d+1);
ans = max(ans, d);
}
int treeDepth(TreeNode* root) {
if (root == NULL) return 0;
ans = 0;
dfs(root, 1);
return ans;
}
private:
int ans;
};
牛逼啊哥