C solution


  • 0
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    int count=0,max=0;
    void preorder_traversal(struct TreeNode* root)
    {
        if (root!=NULL)
        {
            count++;
            preorder_traversal(root->left);
            preorder_traversal(root->right);
            count--;
        }
        else
        {   
            if (count>max)
            {   
                max=count;
                printf ("%d",count);
            }
        }
    }
    int maxDepth(struct TreeNode* root)
    {
        max=0;
        preorder_traversal(root);
        return(max);
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.