leetcode 之Maximum Depth of Binary Tree 用 C语言实现

来源:互联网 发布:mac brew install jdk 编辑:程序博客网 时间:2024/06/07 18:04
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int maxDepth(struct TreeNode* root) {
    if (root == NULL)
            return 0; 
         else if (root->left == NULL && root->right == NULL) 
             return 1;  
         else {
             int left = maxDepth(root->left);
             int right = maxDepth(root->right); 
             return 1 + (left > right ? left : right); 
             
         } 
}
0 0