DEV Community

Somuya Khandelwal
Somuya Khandelwal

Posted on

DAY 9 Conquering Binary Tree Challenges

Hello Everyone!

I’m Somuya Khandelwal, back with updates from Day 4 of Week 2 in my competitive programming journey. Today’s focus was on Binary Trees, a fundamental data structure that appears often in technical challenges and real-world applications. Whether it’s calculating paths or implementing efficient traversals, binary trees offer so many problems that require creative and logical thinking.


What I Worked On Today

  1. Path Sum (Easy Difficulty)

    • Task: Check if there is a root-to-leaf path in the tree where the sum of node values equals a target value.
    • Approach:
      • Used recursive depth-first search (DFS) to explore all paths from root to leaf.
    • What I Learned:
      • DFS is perfect for exploring trees when you need to find all paths.
      • Managing variables like the running sum across recursive calls is critical for correct results (messed this up once but debugged it quickly!).
  2. Sum Root to Leaf Numbers (Medium Difficulty)

    • Task: Calculate the total of all numbers formed by root-to-leaf paths in the tree.
    • Approach:
      • Used recursion to construct the number at each level of the tree and added it up at the leaf nodes.
    • What I Learned:
      • Recursive algorithms are great for these types of cumulative calculations.
      • Keeping track of intermediate states while backtracking is tricky but important—it took me a few tries to get it right.
  3. Binary Tree Maximum Path Sum (Hard Difficulty)

    • Task: Find the maximum path sum in a binary tree, where the path can start and end at any node.
    • Approach:
      • Used post-order traversal to calculate the maximum contribution from each subtree.
      • Updated a global variable to track the highest path sum during traversal.
    • What I Learned:
      • Recursive functions can be designed to return helpful data (like maximum gain) to parent nodes.
      • Balancing global updates with local computations was tough—I had to revisit my logic a couple of times to fix it.
  4. Binary Search Tree Iterator (Medium Difficulty)

    • Task: Create an iterator for a binary search tree (BST) with efficient next() and hasNext() methods.
    • Approach:
      • Used a stack to simulate in-order traversal, pushing nodes lazily as needed.
    • What I Learned:
      • Stacks are super useful for implementing iterative traversals without needing extra space to store the entire tree.
      • Understanding in-order traversal mechanics is essential for working effectively with BSTs.

Key Takeaways from Today

  1. Mastering Traversals:

    • Techniques like DFS, post-order traversal, and in-order traversal are vital for solving binary tree problems.
  2. Managing Recursive States:

    • Problems like Binary Tree Maximum Path Sum showed me how critical it is to track intermediate results and states during recursion.
  3. Efficiency in Tree Operations:

    • The BST Iterator taught me how combining stacks with lazy traversal can make solutions both time and memory efficient.
  4. Global and Local State Balance:

    • Differentiating between global results and local computations is key—this tripped me up initially in the Binary Tree Maximum Path Sum problem.

Reflections and Challenges

The Binary Tree Maximum Path Sum was easily the hardest problem of the day. I found it challenging to balance the recursive logic while maintaining a global maximum. It took a couple of debugging sessions to get it working properly, but the satisfaction of finally cracking it made the effort worthwhile. These problems really strengthened my understanding of tree traversal and recursion.


Looking Ahead

Tomorrow is the last day of the week, and I’ll be working on 1D Dynamic Programming problems like Climbing Stairs and House Robber. These tasks will push me to think about state-based solutions and how to optimize problem-solving.

Thanks for following along on my journey! Stay tuned for more updates as I continue learning, solving, and growing in competitive programming.

Best,

Somuya

Top comments (0)