首页 > 代码库 > Maximum Depth of Binary Tree

Maximum Depth of Binary Tree

问题:二叉树的最深深度

class Solution{public:    void dfs(TreeNode *root,int step,int &MAX)    {        if(root==NULL)        {            if(MAX<step) MAX=step;            return ;        }        dfs(root->left,step+1);        dfs(root->right,step+1);    }    int maxDepth(TreeNode *root)    {        int MAX=0;        dfs(root,step,MAX);        return MAX;    }};

 

class Solution {public:    int maxDepth(TreeNode *root) {        if(root==NULL) return 0;        int leftDepth=maxDepth(root->left);        int rightDepth=maxDepth(root->right);        return max(leftDepth,rightDepth)+1;    }};