Solved LeetCode #102 with Recursive Approach

🚀 Just Solved LeetCode #102 — Binary Tree Level Order Traversal 📘 Problem: Given the root of a binary tree, return the level order traversal of its nodes' values — meaning, traverse the tree level by level from left to right. Example: Input → [3,9,20,null,null,15,7] Output → [[3], [9,20], [15,7]] 🧠 My Approach: I solved this using a recursive approach instead of the usual queue-based BFS. 1️⃣ First, I calculated the total number of levels in the tree using recursion. 2️⃣ Then, for each level, I called a helper function `nThLevel()` to collect all nodes at that level. 3️⃣ I stored each level’s nodes in a separate list and finally returned a list of lists as the result. This approach helped me deeply understand the relationship between recursion depth and tree levels. 💡 What I Learned: ✅ Different ways to perform level order traversal — BFS vs recursion. ✅ How to effectively use recursion to explore each tree level. ✅ Improved understanding of function calls, base conditions, and data storage in recursive algorithms. #LeetCode #Java #DSA #BinaryTree #CodingJourney

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories