代码改变世界

leetcode - Maximum Depth of Binary Tree

2013-04-28 19:22  张汉生  阅读(163)  评论(0编辑  收藏  举报

题目描述:点击此处

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
  int maxInt(int a, int b){
    return a >= b ? a : b;
  }
  int maxDepth(TreeNode *root) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if (root == NULL)
      return 0;
    return maxInt(maxDepth(root->left),maxDepth( root->right)) + 1;
  }
};