标签:des style blog io ar color sp on div
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
标签:des style blog io ar color sp on div
原文地址:http://www.cnblogs.com/Kai-Xing/p/4147397.html