Day 57/100 of #100DaysOfCode 🚀 “Count Good Nodes in a Binary Tree” problem on LeetCode.. A node is considered good if there is no node with a greater value on the path from the root to that node. Approach: ->Traversed the tree using recursion ->Passed the maximum value seen so far along the root-to-node path ->Compared the current node’s value with the max: ->If node.val >= max, it’s a good node ->Updated the max value before going deeper ->Recursively counted good nodes in left and right subtrees ->Summed up the counts to get the final result #Day57 #100DaysOfCode #LeetCode #BinaryTree #DFS #Recursion #Java #DSA #ProblemSolving #LearningEveryDay
Counting Good Nodes in a Binary Tree with Recursion
More Relevant Posts
-
Day 3 of Daily DSA 🚀 Solved LeetCode 1: Two Sum Approach: Used a HashMap to store numbers with their indices. For each element, checked if the complement (target - current) already exists. Complexity: • Time: O(n) • Space: O(n) Performance: Runtime: 2 ms (Beats 99.15%) Memory: 47.34 MB Focusing on writing clean and efficient solutions before over-optimizing. Consistency > Intensity 💯 #DSA #LeetCode #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
📅 Day 73 of #100DaysOfLeetCode 🧩 Problem: 3314. Construct the Minimum Bitwise Array I 📊 Difficulty: Easy 🧠 Key Insight For each prime number nums[i], we need to find the smallest non-negative integer x such that: x | (x + 1) == nums[i]. If no such value exists, the answer should be -1. ⚙️ Approach Initialize the answer array with -1 For each number nums[i]: Try all values of x from 0 to nums[i] - 1 Check if x | (x + 1) equals nums[i] As soon as a valid x is found, store it and stop searching If no valid value is found, keep -1 for that index ⏱ Complexity Time: O(n × max(nums[i])) Space: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
Day 58/100 of #100DaysOfCode 🚀 Minimum Absolute Difference in a BST (Leetcode) For a Binary Search Tree, an in-order traversal produces values in sorted order.So, the minimum absolute difference will always be between two consecutive nodes in this order. Approach: Used DFS (in-order traversal): left → root → right Maintained: prev → previously visited node value minDifference → smallest difference found so far At each node: Calculated root.val - prev (only if prev exists) Updated minDifference with the minimum value Updated prev after visiting the current node. #Day58 #100DaysOfCode #LeetCode #BST #BinaryTree #InorderTraversal #DFS #Java #DSA #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 47/50 of #50DaysOfDSA 🚀 Today’s focus was on string manipulation and stack-based logic with LeetCode 1614 — Maximum Nesting Depth of the Parentheses. The Problem: Given a valid parentheses string, the goal is to find the maximum nesting depth. My Approach: Instead of using an actual stack data structure, I used a simple counter to track the current depth: Increment the counter when encountering ( Decrement when encountering ) Track the maximum value the counter reaches during the iteration. The Result: ✅ Runtime: 0 ms (Beats 100.00% of Java submissions!) ✅ Complexity: O(n) time and O(1) space. It’s satisfying to see how a simple linear scan can be so performant. Almost at the finish line! 🏁 #DSA #CodingChallenge #Java #LeetCode #ProblemSolving #SoftwareEngineering #Day47
To view or add a comment, sign in
-
-
Day: 21/365 Problem: Minimum Cost Path with Edge Reversals Medium Key takeaways / learnings: 1. Graph problems can often be transformed by redefining edge weights to model constraints like reversals or penalties. 2. Dijkstra works even when the graph is transformed, as long as all edge weights are non-negative. 3. Thinking in terms of cost instead of steps helps solve non-standard shortest path problems. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #Consistency 🥷
To view or add a comment, sign in
-
-
Day 7 of Daily DSA 🚀 Solved LeetCode 167: Two Sum II – Input Array Is Sorted Approach: Used the two-pointer technique leveraging the sorted nature of the array. Moved pointers inward based on the comparison with the target to find the answer in one pass. Complexity: • Time: O(n) • Space: O(1) (constant extra space) LeetCode Stats: • Runtime: 2 ms (Beats 96.10%) • Memory: 48.54 MB (Beats 38.87%) This problem highlights how understanding input constraints can turn a brute-force solution into an optimal one. #DSA #LeetCode #Java #TwoPointers #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
#day306 of #1001daysofcode problem statement (0110): Balanced Binary Tree Solved “Balance a Binary Search Tree” today. Approach: 1. Perform inorder traversal to extract sorted values. 2. Rebuild the BST by picking the middle element recursively. Clean two-step transformation: flatten → rebuild. #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 15 of #200daysofdsa LeetCode 13 | Roman to Integer (Java) Solved this problem using a HashMap + traversal from right to left. Key logic: If current value < next value → subtract Else → add *>Time Complexity: O(n) *>Space Complexity: O(1) (fixed map size) *>Clean handling of subtractive cases like IV, IX, XL, XC, CD, CM Consistency is the real progress #LeetCode #DSA #Java #Strings #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🔥Day 91 of #100DaysOfLeetCode Problem: 1653. Minimum Deletions to Make String Balanced Difficulty: Medium Key Insight: A balanced string must have all 'a's before all 'b's. Any 'b' before an 'a' creates a violation. Approach: Preprocess the string using: - prefixB[i]: number of 'b's strictly before index i - suffixA[i]: number of 'a's strictly after index i For every index i, treat it as a split point and compute: deletions = prefixB[i] + suffixA[i] The minimum over all splits is the answer. Time Complexity: O(n) Space Complexity: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
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