Maximum Width of Binary Tree


  • 0

    Click here to see the full article post


  • 0
    D

    Was that really accepted? I wonder about the case when the input is a tree always going to the right. As far as I understand, your 'pos' integer variable would overflow with a depth > 32. I am asking it because it happened to me.


  • 0

    @dantegt I test every solution I post against the judge beforehand. The question statement kind of implies pos fits 32-bit integer, even though it doesn't exactly say that.


  • 0
    D

    @awice Ohhh, it's Java, and it does not throw exception when the integer overflows. I implemented my solution in C#, which throws exception. Your Java code seems to magically work, even though the 'pos' variable overflows and becomes negative. Maybe if I had used the 'unchecked' keyword, my C# code would have worked as well.

    Try the following tree (or a longer one), and you will see 'pos' variable becoming negative. Actually, the judge tries a much longer one.

    [0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null,0,null, 0, null, 0, null, 0,null,0,null,0,null, 0, null, 0, null,0,null, 0, null, 0, null, 0,null,0,null,0,null, 0, null,0, null, 0]


  • 0
    D

    What is (depth, x-> pos)?? Why not just (depth, pos)


Log in to reply
 

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