1. 程式人生 > >【LeetCode】maximum-depth-of-binary-tree

【LeetCode】maximum-depth-of-binary-tree

[程式設計題] maximum-depth-of-binary-tree

時間限制:1秒

空間限制:32768K

 

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.

/**
 * 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 maxDepth(TreeNode *root) {
         if(root==NULL)return 0;
         else {
                int t1=maxDepth(root->left);
                int t2=maxDepth(root->right);
                return 1+max(t1,t2); 
               }
       }
};