Day 39/45 of my #SDESheetChallenge journey by
@takeUforward_
Continued practising binary trees today and solved 4 problems focused on recursion, tree restructuring, and property validation:
1️⃣ Symmetric Tree
Compared the left and right subtrees in mirror order using recursion.
2️⃣ Flatten Binary Tree to Linked List
Used an iterative in-place approach to restructure the tree into a right-skewed linked list.
3️⃣ Children Sum Property
Recursively verified whether every non-leaf node equals the sum of its children.
4️⃣ Symmetric Binary Tree
Reinforced the mirror-tree pattern by recursively comparing opposite child nodes.
Key learnings:
→ Mirror recursion: left -right
→ In-place tree modification
→ Recursive property validation
→ Handling null and leaf-node base cases correctly
Day 38/45 of my #SDESheetChallenge journey by
@takeUforward_
Continued my Binary Trees journey
Solved:
1️⃣ Binary Tree Maximum Path Sum
2️⃣ Construct Binary Tree from Preorder & Inorder
3️⃣ Construct Binary Tree from Inorder & Postorder
Key learnings:
→ Tree DP and recursive path contributions
→ Handling negative paths
→ Using traversal properties to reconstruct trees
→ Mapping inorder indices for efficient subtree construction
Getting more comfortable with identifying what each recursive function should return vs what needs to be tracked globally.
#DSA #LeetCode #CPP #BinaryTrees #CodingJourney
Day 37/45 of my #SDESheetChallenge journey by
@takeUforward_
Continued my Binary Trees journey by solving 4 important problems:
1️⃣ Lowest Common Ancestor of a Binary Tree
→ Used recursion to search both subtrees
→ If p and q are found on different sides, the current node is the LCA
2️⃣ Same Tree
→ Recursively compared the structure and values of both trees
→ Both left and right subtrees must be identical
3️⃣ Boundary Traversal of Binary Tree
→ Divided the traversal into three parts:
• Left boundary
• Leaf nodes
• Right boundary in reverse order
4️⃣ Binary Tree Zigzag Level Order Traversal
→ Used BFS with a queue
→ Alternated the insertion direction at every level
Binary Tree problems become much easier when the traversal is broken into clear recursive or level-wise subproblems.
After 37 days feels great to be consistent.
#DSA #BinaryTrees #LeetCode #GeeksForGeeks #Cpp
Day 36/45 of my #SDESheetChallenge journey by @takeUforward_
Today I revised 4 important Binary Tree problems:
✅ Level Order Traversal — BFS + Queue
✅ Maximum Depth of Binary Tree — Recursion
✅ Diameter of Binary Tree — Height + DFS
✅ Balanced Binary Tree — Recursion + Height
Key points:
• BFS → when processing trees level by level
• Recursion → when solving the same problem for left and right subtrees
• Height → a building block for many tree problems
Slowly getting closer to 45 day mark.
#DSA #LeetCode #CPP #BinaryTree #Coding
Day 35/45 of the #SDESheetChallenge by @takeUforward_ completed!
Solved 4 Binary Tree problems today:
🔹 All Traversals in One Traversal
Pattern: Iterative DFS + Stack + Node State
Key Idea: Store each node with a state (1, 2, or 3) to generate preorder, inorder, and postorder in a single traversal.
T.C: O(N) | S.C: O(N)
🔹 Vertical Order Traversal
Pattern: DFS + Coordinate Mapping
Key Idea: Assign each node a column and row, group nodes by coordinates, and sort nodes sharing the same position.
T.C: O(N log N) | S.C: O(N)
🔹 Root to Leaf Paths
Pattern: DFS + Backtracking
Key Idea: Build the current path during DFS and store it whenever a leaf node is reached.
T.C: O(N × H) | S.C: O(H)
🔹 Maximum Width of Binary Tree
Pattern: Level Order Traversal + Indexing
Key Idea: Assign virtual indices to nodes as if the tree were complete and calculate the width between the first and last node at every level. Normalise indices to prevent overflow.
T.C: O(N) | S.C: O(N)
Binary Tree problems become much easier when you identify what information must travel with each node:
→ State for multiple traversals
→ Coordinates for vertical traversal
→ Path for root-to-leaf problems
→ Index for width calculation
#DSA #LeetCode