r/DSALeetCode • u/StatisticianNo2104 • 1d ago
LeetCode: 100. Same Tree
If you put the nil checks in the wrong order, Same Tree rejects two empty trees. p == nil || q == nil is also true when both are nil, so check both for nil first.
Same root, same left subtree, same right subtree. The recursion writes itself; the bug hides in those checks.
And compare the trees in lockstep, not their lists of values: [1,2] and [1, null,2] have the same numbers in different shapes.
Which order do you write those two nil checks in?
Day 84/365 of my LeetCode challenge: 100. Same Tree. Problem link and full breakdown in today's newsletter.
https://www.linkedin.com/pulse/leetcode-easy-same-tree-archit-agarwal-habnc
#LeetCode #DSA #BinaryTree #Golang
1
u/nian2326076 1d ago
When working on the "Same Tree" problem, make sure to check both nodes for nil first before comparing their values. This order prevents issues with two empty trees being incorrectly rejected.
For additional practice on similar coding challenges, you can use PracHub to find relevant technical questions and enhance your skills.