zoukankan      html  css  js  c++  java
  • 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
Copyright © 2011-2022 走看看