Maximum Depth of Binary Tree

求二叉树的最大深度

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
  * };
 */
 int max(int a,int b){
     return a>b?a:b;
 }
int maxDepth(struct TreeNode *root) {
    if(!root) return 0;
    if(!root->left && !root->right) return 1;
    return max(maxDepth(root->left),maxDepth(root->right))+1;
}

 

posted @ 2015-03-10 22:14  SprayT  阅读(97)  评论(0编辑  收藏  举报