104. Maximum Depth of Binary Tree(C++)

104. Maximum Depth of Binary Tree

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.

题目大意:

给一颗二叉树,求最大深度。

解题方法:

1.利用递归遍历二叉树。

注意事项:

C++代码:

 /**
* 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:
int maxDepth(TreeNode* root)
{
int l,r;
if(root==nullptr) return ;
l=maxDepth(root->left);
r=maxDepth(root->right);
return l>r?l+:r+;
}
};
上一篇:Hello China操作系统STM32移植指南(二)


下一篇:MySQL使用rand函数实现随机数