104. Maximum Depth of Binary Tree

来源:互联网 发布:mp3铃声截取软件 编辑:程序博客网 时间:2024/05/08 23:18
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int max(int x,int y)
    {
        return x>y ? x : y;
    }
    int Depth(TreeNode* node)
    {
        int max_Depth = 0;
        if(node)
        {
            max_Depth++;
            if(node->left || node->right)
                max_Depth += max(Depth(node->left), Depth(node->right));
        }
        return max_Depth; 
    }
    int maxDepth(TreeNode* root) {
        if(!root)
            return 0;
        return Depth(root);
    }

};

【总结】二叉树的深度,基础。

0 0
原创粉丝点击