Maximum Depth of Binary Tree

来源:互联网 发布:2017淘宝成交额 编辑:程序博客网 时间:2024/06/05 12:03
/** * 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 max = -1;    int maxDepth(TreeNode *root ) {        if( root == NULL )            return 0;        return findMax( root, 1 );    }    int findMax( TreeNode *root, int val )    {        if( root == NULL )            return 0;        if( val > max )            max = val;        findMax( root->left, val+1 );        findMax( root->right, val+1 );        return max;    }};

0 0
原创粉丝点击