r/badcode • • Jun 19 '21

typescript Check if binary tree is balanced...

Post image
221 Upvotes

44 comments sorted by

View all comments

123

u/pedro823 Jun 19 '21

Frankly, LGTM. Not hard to understand, variables with somewhat decent names, and no blatant optimizations to be seen.

22

u/SuitableDragonfly Jun 20 '21

Me too. I was sitting here wondering if I was just really stupid to not see how this is bad.

5

u/paulsmithkc 👨‍🏫 Jun 20 '21

This looks like awesome code to me. Well documented, good variable names, super easy to read, easy to test, and sufficiently optimized.

This is a great example of where tuples work well, and how to use them in a recursive function.

High five to whoever wrote the code, I'm sure they'd be be a great team member to have.

6

u/[deleted] Jun 20 '21

In principle there's a way to terminate earlier. As soon as you find an unbalanced subtree you know the second argument is always going to be false

-62

u/MrPancholi Jun 19 '21

It does work, but I just thought that the tuple thing I did was a little weird.

I suck with algorithms so I though this was pretty far form the ideal solution.

69

u/dliwespf Jun 19 '21

Since this seems to be your own code: Don't shame yourself, your code is quite alright.

And additionally: You seem to try to improve your own coding style. So in my opinion you are doing well!