首页 > 代码库 > Leetcode: Maximum Depth of Binary Tree

Leetcode: 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。

1 class Solution {2 public:3     int maxDepth(TreeNode *root) {4         if(!root) return 0;5         return max(maxDepth(root->left), maxDepth(root->right)) + 1;6     }7 };

 

Leetcode: Maximum Depth of Binary Tree