#104 Maximum Depth of Binary Tree

#104 Maximum Depth of Binary Tree

/**
* 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 {
private:
int max = 1;
void godown(TreeNode* node, int depth) {
if(node->left!=NULL) {
godown(node->left, depth+1);
}
if(node->right!=NULL) {
godown(node->right, depth+1);
}
if(node->left==NULL && node->right==NULL) {
if(depth > max) {
max = depth;
}
}
}
public:
int maxDepth(TreeNode* root) {
if (root==NULL) {
return 0;
}
godown(root,1);
return max;
}
};

• 本文已收录于以下专栏：

104/111 Maximum/Minimum Depth of Binary Tree（二叉树求深度）

104 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the...

104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth...

[LeetCode]104.Maximum Depth of Binary Tree

【题目】 Maximum Depth of Binary Tree  Total Accepted: 5260 Total Submissions: 11532My Submissions ...

LeetCode#104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum dep...

【LeetCode】104. Maximum Depth of Binary Tree（DFS|BFS）

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)