Many of us went through the same pain as I did — Recursion. When I started learning DSA concepts and solving Leetcode problems, I first encountered recursion. My reaction was, “How do these 3–4 lines even work?”. I searched for tutorials and tried to understand them. But I could just take the code, run it, accept it and never touched it again for a long time. I solved problems for so long and still I was not able to grasp after it even after 3–4 months of DSA. When it came to trees, it even got worse for me. DFS on a matrix for me was like magic. Throw some if statements and call the function again inside the same function itself.
Then all of a sudden, One day I could finally understand the visualization. Whenever I want to solve a problem. I need that visualization of solution. But recursion never gave me that clarity. Some said think it as a stack of operations one after the other. What hit me was when I did postorder, preorder traversal on binary tree. I could see that it hits the last leaf with no nodes and then pushes it. I could see the base condition and then calling the function to get the leaf node and push it. This was the situation 6 months ago.
Now, I can totally rock it. I solve medium problems of recursion on trees, graphs or matrix within 10–15mins. Today I was on a spree for medium recursion problems. I solved 5 problems in under an hour.I was recalling the moment when i called it magic. Yeah, It was funny to think how struggles are pain in the moment but looking back at them felt trivial or not needed. This hit me when i was solving 814. Binary Tree Pruning. This was the solution for the problem.
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
modifyTree(root);
return root;
}
private:
void modifyTree(TreeNode*& node) {
if (node == nullptr) return;
if (node->right != nullptr) modifyTree(node->right);
if (node->left != nullptr) modifyTree(node->left);
if (node->val == 0 && node->right == nullptr && node->left == nullptr) node = nullptr;
}
};
Get the last leaf of the tree and then check whether its value is 0 and has no left or right child nodes. Then point it to nullptr. Then the same check happens for the parent nodes, as the recursion completes from the bottom up…
You could be one of the people who is stuck in these kind of problems. Maybe it‘s Sliding window, Two pointers, DP or even the classic Two sum problem. Remember just try to hit it again tomorrow and keep doing it. It will be difficult at first to manage the feeling of being dumb. That’s how it should be. When you workout, you feel weak afterward but later the next day you become stronger. It’s the same case with learning. It applies to any kind of subject you are learning whether it’s DSA, Web development, App development or System design.
Progress is not always linear; It goes up and down. Sometimes it feels like the chart has crashed. But the effort you put in never goes in vain. Just hit it consistently. Comment down your thoughts and if you found something useful, Feel free to follow!
Top comments (0)