🚀 Day 64 of #100DaysOfCode Solved 222. Count Complete Tree Nodes on LeetCode 🔗 🧠 Key Insight: In a complete binary tree, all levels are fully filled except possibly the last, and nodes are as left as possible. 👉 This property helps us optimize beyond simple traversal ⚙️ Approach (Simple DFS - Your Solution): 1️⃣ If root is null → return 0 2️⃣ Recursively count: 🔹 left = countNodes(root.left) 🔹 right = countNodes(root.right) 3️⃣ Total nodes: 👉 1 + left + right ⏱️ Time Complexity: Current → O(n) Optimized → O(log² n) 📦 Space Complexity: O(h) #100DaysOfCode #LeetCode #DSA #BinaryTree #Recursion #DivideAndConquer #Java #InterviewPrep #CodingJourney
Counting Complete Binary Tree Nodes on LeetCode
More Relevant Posts
-
𝐃𝐚𝐲 𝟔𝟑 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on rotating an array to the right by k steps efficiently. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Rotate Array 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐑𝐞𝐯𝐞𝐫𝐬𝐚𝐥 𝐓𝐞𝐜𝐡𝐧𝐢𝐪𝐮𝐞 • First normalized k using k % n • Reversed the entire array • Reversed the first k elements • Reversed the remaining elements This results in the desired rotation. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Reversal technique is a powerful in-place trick • Breaking a problem into steps simplifies logic • Modulo helps handle large values of k • In-place operations reduce space complexity 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the best solution is not shifting elements — but rearranging them smartly. 63 days consistent 🚀 On to Day 64. #DSA #Arrays #TwoPointers #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 Day 62 of #100DaysOfCode 🌱 Topic: Trees / Recursion ✅ Problem Solved: LeetCode 112 – Path Sum 🛠 Approach: Used DFS (recursion) to explore all root-to-leaf paths. Base Case: If node is null → return false If leaf node: Check if target == node.val → return result Otherwise: Reduce target → target - node.val Recurse on left and right subtree #100DaysOfCode #Day62 #DSA #Trees #Recursion #DFS #LeetCode #Java #BinaryTree #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 65/100 🚀 | #100DaysOfDSA Solved LeetCode 148 – Sort List today. The problem was to sort a linked list in O(n log n) time and constant space complexity. Approach: Used Merge Sort on Linked List, since it works efficiently with linked structures. • Base case: if the list is empty or has one node, return it. • Used slow and fast pointers to find the middle of the list. • Split the list into two halves. • Recursively sorted both halves. • Merged the two sorted halves using a helper merge function. The merge step compares nodes and builds a sorted list using a dummy node. Time Complexity: O(n log n) Space Complexity: O(log n) (due to recursion stack) Key takeaway: Merge Sort is the best choice for linked lists because it avoids random access and works efficiently by splitting and merging nodes. #100DaysOfDSA #LeetCode #DSA #Java #LinkedList #MergeSort #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🔥 Day 60 / 100 – LeetCode Challenge ✅ Solved: 160. Intersection of Two Linked Lists Today’s problem was all about understanding pointer behavior and memory references in linked lists. 💡 Key Insight: Instead of comparing values, we compare node references. If two linked lists intersect, they will share the same tail nodes. 🚀 Approach Used (Optimal): Two pointers (pA, pB) Traverse both lists When one reaches the end, switch to the other list They eventually meet at the intersection node (or null) 🧠 Why it works: Both pointers travel equal distance → LengthA + LengthB, aligning perfectly without extra space. ⏱ Complexity: Time: O(m + n) Space: O(1) 💻 Result: ✔️ Accepted (41/41 test cases) ⚡ Runtime: 1 ms (Beats 99.94%) 📌 Takeaway: Sometimes the smartest solution is not about extra data structures, but about clever traversal. #Day60 #100DaysOfCode #LeetCode #Java #DSA #LinkedList #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 61 of #100DaysOfCode Solved 101. Symmetric Tree on LeetCode 🔗 🧠 Key Insight: A tree is symmetric if it is a mirror of itself 👉 Left subtree is a mirror of right subtree ⚙️ Approach (Recursive Mirror Check): 1️⃣ Start by comparing: 🔹 root.left and root.right 2️⃣ If both are null → ✅ symmetric 3️⃣ If one is null → ❌ not symmetric 4️⃣ If values differ → ❌ not symmetric 5️⃣ Recursively check mirror condition: 🔹 left.left with right.right 🔹 left.right with right.left 6️⃣ Return: 👉 isMirror(left.left, right.right) && isMirror(left.right, right.left) ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) #100DaysOfCode #LeetCode #DSA #BinaryTree #Recursion #DFS #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Day 41 of Daily DSA 🚀 Solved LeetCode 20: Valid Parentheses ✅ Problem: Given a string containing only (), {}, [], determine if the input string is valid. Rules: Open brackets must be closed by the same type Open brackets must be closed in the correct order Every closing bracket must have a matching opening bracket Approach: Used a Stack to track opening brackets and validate matching pairs. Steps: Traverse the string Push opening brackets onto the stack For closing brackets → check top of stack If it matches → pop Else → return false At the end, stack should be empty ⏱ Complexity: • Time: O(n) • Space: O(n) 📊 LeetCode Stats: • Runtime: 3 ms (Beats 87.41%) ⚡ • Memory: 43.37 MB A classic stack problem that builds strong fundamentals for expression parsing & validation. #DSA #LeetCode #Java #Stack #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Today I solved LeetCode 543 – Diameter of Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, return the diameter of the tree. The diameter is defined as the length of the longest path between any two nodes in the tree. This path may or may not pass through the root. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Height calculation 🧠 Approach: Use DFS to calculate the height of each subtree. For every node: Compute left subtree height Compute right subtree height The diameter at that node = left height + right height Keep track of the maximum diameter seen so far. Return the height of the current node: 1 + max(left height, right height) ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: How to compute multiple values (height + diameter) in one traversal. Understanding that diameter doesn’t always pass through root. Optimizing recursive tree problems. Strengthening DFS and recursion concepts. Tree problems are sharpening my problem-solving skills Consistency and daily practice make the difference #LeetCode #DSA #BinaryTree #DiameterOfBinaryTree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Day 113 - LeetCode Journey Solved LeetCode 222 – Count Complete Tree Nodes ✅ This problem involves counting the total number of nodes in a complete binary tree, where all levels are fully filled except possibly the last, and nodes are as far left as possible. Approach: I implemented a recursive solution to count nodes. For each node, I recursively calculated the number of nodes in the left and right subtrees and added 1 for the current node. Although this solution works correctly, it follows a straightforward traversal approach and does not fully utilize the properties of a complete binary tree. A more optimized approach can reduce time complexity by comparing left and right subtree heights to detect perfect subtrees and compute their node count directly. Complexity Analysis: • Time Complexity: O(n) • Space Complexity: O(h), where h is the height of the tree (recursion stack) Key Takeaways: • Basic tree traversal (DFS) can solve counting problems effectively • Understanding tree properties can help in optimizing solutions further • Complete binary trees allow more efficient approaches than general trees All test cases passed successfully 🎯 #LeetCode #DSA #BinaryTree #Recursion #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 77/100 🚀 | #100DaysOfDSA Solved LeetCode 349 – Intersection of Two Arrays today. The problem was to find the intersection of two arrays, returning only unique elements present in both. Approach: Used HashSet for efficient lookup and uniqueness. • Stored all elements of nums1 in a HashSet • Iterated through nums2 and checked if elements exist in the set • If yes, added them to a result HashSet (to avoid duplicates) • Converted the result set into an array This ensures only common and unique elements are included . Time Complexity: O(n + m) Space Complexity: O(n) Key takeaway: HashSet is very useful when dealing with uniqueness + fast lookup, especially in intersection-type problems. #100DaysOfDSA #LeetCode #DSA #Java #HashSet #Arrays #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 75 of #100DaysOfCode Solved 103. Binary Tree Zigzag Level Order Traversal on LeetCode 🔗 🧠 Key Insight: This is a variation of level order traversal where: 👉 Levels alternate between left → right and right → left ⚙️ Approach (BFS + Direction Toggle): 1️⃣ Use a queue for level order traversal 2️⃣ Maintain a flag (leftToRight or sign) 🔹 true → left → right 🔹 false → right → left 3️⃣ For each level: 🔹 Create a list 🔹 Traverse all nodes in that level 4️⃣ Insert values based on direction: 🔹 If left → right → addLast(val) 🔹 Else → addFirst(val) 5️⃣ Add level list to result 6️⃣ Flip direction: 👉 sign *= -1 or toggle boolean ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #100DaysOfCode #LeetCode #DSA #BinaryTree #BFS #Queue #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development