C++ Solution, depth


  • 0
    S
    class Solution {
    public:
        int diameterOfBinaryTree(TreeNode* root) {
            depth(root);
            return maxVal;
        }
        int maxVal = 0;
        int depth(TreeNode* tree) {
            if(tree == nullptr)
            return 0;
            int left = 0, right = 0;
            if(tree->left)
                left = depth(tree->left)+1;
            if(tree->right)
                right = depth(tree->right)+1;
            int total = left+right;
            maxVal = max(maxVal,total);
            return max(left,right);
        }
    };
    

Log in to reply
 

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