r/LeetcodeChallenge • • 2d ago

STREAK🔥🔥🔥 LeetCode: 814. Binary Tree Pruning

Post image

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

2 Upvotes

1 comment sorted by

1

u/nian2326076 1d ago

For a deeper understanding of binary trees and to enhance your preparation, you can use PracHub for a variety of technical interview questions. This platform can help you reinforce your skills and familiarize yourself with different problem types, which will be beneficial as you work on challenges like binary tree pruning.