r/DSALeetCode • u/StatisticianNo2104 • 1d ago
LeetCode: 814. Binary Tree Pruning
Setting node = nil inside a recursive call deletes nothing. The caller never sees it. A node can't remove itself because it has no reference to its parent.
A node survives if its subtree holds a 1 anywhere, which, recursively, is almost the definition read aloud. Post-order, because a node can't answer that on the way down.
So the child reports "nothing worth keeping here", and the parent clears the pointer. That leaves the root, which has no parent, and that's the only reason the wrapper function exists.
Have you ever nilled a local variable and expected the caller to notice?
Day 73/365 of my LeetCode challenge: 814. Binary Tree Pruning. Problem link and full breakdown in today's newsletter.
https://www.linkedin.com/pulse/leetcode-binary-tree-pruning-archit-agarwal-g6lsc
#LeetCode #DSA #BinaryTree #Golang
1
u/nian2326076 1d ago
Binary tree pruning is a complex topic. To deepen your understanding, consider practicing with interview questions on data structures and algorithms via PracHub. This resource can help you work through similar problems and prepare for technical interviews effectively.