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;
    }
};

  

原文地址:https://www.cnblogs.com/zsboy/p/3885811.html