leetCode题解之求二叉树最大深度

1、题目描述

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,依次类推。最大深度就是离根最远的节点的深度。

 

2、问题分析

对于二叉树而言,使用递归是最容易的一种解法。

 

3、代码

int maxDepth(TreeNode* root)
     {
         if(root == NULL)
             return 0;
         else
             return max( 1+maxDepth(root->left) , 1+maxDepth(root->right) ) ;
     }

 

posted @ 2018-03-29 14:26  山里的小勇子  阅读(629)  评论(0)    收藏  举报