error between return max(initializer_list<T>) and max(a,max(b,c))


  • 0
    S

    The example is just [1,3,2,5,3,null,9].

    The code in the end can be accepted. But when I change the last row by

     return max(order-v[level]+1, max(DFS(root->left, level+1, 2*order, v), DFS(root->right, level+1, 2*order+1, v)));
    

    Run the code with the example above, I only get the answer 1 while the correct one is 4.
    I don't find whether max in the tail recursion will result error.
    Hope for someone's help.

    class Solution {
    public:
        int widthOfBinaryTree(TreeNode* root) {
            return DFS(root, 0, 1, vector<int>()={});
        }
    
    private:
        int DFS(TreeNode *root, int level, int order, vector<int>& v)
        {
            if(!root)
                return 0;
            if(v.size() == level)
                v.push_back(order);
            return max({order-v[level]+1, DFS(root->left, level+1, 2*order, v), DFS(root->right, level+1, 2*order+1, v)});
        }
    };
    

Log in to reply
 

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