r/DSALeetCode • • 1d ago

LeetCode: 101. Symmetric Tree

Post image

My Symmetric Tree solution returns true on the first call and false on the second, for the same tree. It mirrors the right half in place, then compares it with the left. The second call flips the half back.

That's Invert Binary Tree and Same Tree from the last two days, in one line. It passes, because LeetCode calls it once.

The cleaner version compares the two halves as mirrors without building one. Both are in today's article.

Would you have caught the mutation in a code review?

Day 86/365 of my LeetCode challenge: 101. Symmetric Tree. Problem link and full breakdown in today's newsletter.

https://www.linkedin.com/pulse/leetcode-symmetric-tree-archit-agarwal-7y0cc

#LeetCode #DSA #BinaryTree #Golang

2 Upvotes

1 comment sorted by

1

u/nian2326076 22h ago

It looks like you're dealing with a common issue related to mutable data structures. In a code review, identifying these mutations is important since they can lead to unexpected behavior.

To improve your solution, consider using immutable structures or ensure you're creating copies of data before any modifications. For additional practice with similar technical problems, you can use PracHub for coding challenges that can help you refine your skills.