Day 89/100 – #100DaysOfCode 🚀 | #Java #BinaryTree #BFS ✅ Problem Solved: Find Bottom Left Tree Value (LeetCode 513) 🧩 Problem Summary: Given the root of a binary tree, return the leftmost value in the last row of the tree. 💡 Approach Used: ✔ Used Level Order Traversal (BFS) ✔ Traverse the tree level by level ✔ For each level, store the first (leftmost) node value ✔ The value from the last level is the answer This guarantees we always capture the correct bottom-left value. ⚙ Time Complexity: O(N) 📦 Space Complexity: O(N) (queue for BFS) ✨ Takeaway: Level-order traversal is ideal when problems involve row-wise processing or depth-based values in trees. #Java #LeetCode #BinaryTree #BFS #100DaysOfCode #CodingChallenge
Java Binary Tree BFS Solution for Leftmost Value
More Relevant Posts
-
Day 91/100 – #100DaysOfCode 🚀 | #Java #BinaryTree #BFS ✅ Problem Solved: Find Largest Value in Each Tree Row (LeetCode) 🧩 Problem Summary: Given the root of a binary tree, return an array containing the largest value in each level (row) of the tree. 💡 Approach Used: ✔ Used Breadth-First Search (Level Order Traversal) ✔ Process the tree level by level using a queue For each level: Initialize maxValue to the smallest possible integer Traverse all nodes at that level Update maxValue Add it to the result list ⚙ Time Complexity: O(N) 📦 Space Complexity: O(W) (where W = maximum width of the tree) ✨ Takeaway: Level-order traversal is ideal when problems require row-wise processing in trees — clean, intuitive, and efficient. #Java #LeetCode #BinaryTree #BFS #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 86/100 – #100DaysOfCode 🚀 | #Java #Tree #DFS ✅ Problem Solved: Most Frequent Subtree Sum (LeetCode 508) 🧩 Problem Summary Given the root of a binary tree, compute the sum of every subtree and return the value(s) that occur most frequently. A subtree sum is defined as the sum of all node values in that subtree, including the node itself. 💡 Approach Used ✔ Use postorder DFS to compute subtree sums ✔ For each node: SubtreeSum = leftSum + rightSum + node.val ✔ Store frequency of each sum using a hashmap ✔ Track the maximum frequency ✔ Collect all sums whose frequency equals the maximum ⚙ Time Complexity: O(N) 📦 Space Complexity: O(N) ✨ Takeaway Postorder traversal is ideal when child computations are required before processing the parent — a classic and powerful tree pattern. #Java #LeetCode #BinaryTree #DFS #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 80/100 – #100DaysOfCode 🚀 | #Java #BinarySearchTree #DFS ✅ Problem Solved: Find Mode in Binary Search Tree (LeetCode) 🧩 Problem Summary Given the root of a Binary Search Tree, return all the mode(s) — the value(s) that appear most frequently in the tree. 📌 Note: BST property → inorder traversal gives sorted order There may be multiple modes 💡 Approach Used ✔ Use inorder traversal (DFS) to process values in sorted order ✔ Track: prev → previous node value count → current frequency maxCount → highest frequency seen so far ✔ Logic: If current value == prev → increment count Else → reset count to 1 Update result list when: count > maxCount → clear list & add value count == maxCount → add value This avoids extra space like HashMaps by leveraging BST properties. ⚙ Time Complexity: O(N) 📦 Space Complexity: O(H) (recursion stack) ✨ Takeaway Understanding tree properties (like sorted inorder traversal in BST) can eliminate unnecessary data structures and lead to cleaner solutions. #Java #LeetCode #BST #DFS #TreeTraversal #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
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
To view or add a comment, sign in
-
-
When I first learned Tree data structures in Java, I felt lost. Arrays were easy. Lists were predictable. Everything was linear. Then Trees showed up. 1 / \ 2 3 / \ / \ 4 5 6 7 I just don't know how to read this. If you’ve ever felt the same confusion, I wrote a detailed breakdown, starting from counting nodes, understanding levels, all the way to inOrder traversal. 👉 Read the full explanation on my website here: https://lnkd.in/g6kJjRTC #Java #DataStructures #BinaryTree #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
Day 97/100 – #100DaysOfCode 🚀 | #Java #Strings #Logic ✅ Problem Solved: Longest Uncommon Subsequence I (LeetCode) 🧩 Problem Summary: Given two strings, find the length of the longest uncommon subsequence — a subsequence that appears in one string but not in the other. 💡 Approach Used: ✔ Observational / logical approach ✔ If both strings are equal, no uncommon subsequence exists ✔ If they are different, the longer string itself is the answer Why it works: A string is always a subsequence of itself If strings differ, the longer one cannot be a subsequence of the shorter ⚙ Time Complexity: O(1) 📦 Space Complexity: O(1) ✨ Takeaway: Some problems look like DP but are solved with pure logic. Always check for simple observations before overengineering. #Java #LeetCode #Strings #Logic #100DaysOfCode #CodingChallenge
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
-
-
Revising arrays in Java to store, access, and manipulate collections of data efficiently. Building stronger problem-solving foundations. #Revising #Day7 #Java #CoreJava #Arrays #LearningJourney #Consistency
To view or add a comment, sign in
-
#200DaysOfCode – Day 108 Combination Sum III Problem:- Combination Sum III Task:- Find all valid combinations of k numbers that sum up to n, using only numbers from 1 to 9, where: Each number is used at most once No duplicate combinations are allowed Example: Input: k = 3, n = 7 Output: [[1, 2, 4]] My Approach:- Used Backtracking (DFS) to explore all possible combinations. Started from a given number to avoid duplicates. Stopped recursion when: The combination size exceeded k The sum became negative Added the combination to the result only when: Exactly k numbers were chosen The sum became 0 Time Complexity:- Exponential (bounded due to range 1–9) Space Complexity:- O(k) (recursive stack + temporary list) Backtracking may look complex at first, but with clear base conditions and pruning, it becomes a powerful and elegant tool for solving combination problems efficiently. #takeUforward #200DaysOfCode #Java #ProblemSolving #LeetCode #Backtracking #Recursion #DSA #CodingJourney #CodeNewbie
To view or add a comment, sign in
-
-
🚀 Day 3 / 100 – #100DaysOfCode Today’s focus was on Java backend fundamentals and deep DSA understanding. ⸻ 🔧 Development (Java) 📌 Local DB using JSON • Used JSON as a lightweight local database • Applied Java Serialization to convert objects ↔ JSON • Linked serialized JSON data with Java classes for persistent storage • Helpful for small-scale apps where a full DB is overkill 🧠 DSA Progress 1️⃣ Set Matrix Zeroes Problem: If an element is 0, set its entire row and column to 0 Optimized Approach (O(1) Space): • Use first row & first column as markers • If matrix[i][j] == 0: • Mark matrix[i][0] = 0 • Mark matrix[0][j] = 0 • Maintain two flags: • rowZero → whether first row contains 0 • colZero → whether first column contains 0 • Traverse matrix (excluding first row/column) and update based on markers • Finally, update first row & column using flags 2️⃣ Word Search Problem: Check if a word exists in a 2D board Approach: • Start DFS from cells matching first character • Move in 4 directions (up, down, left, right) • Use backtracking: • Mark cell as visited • Revert after recursive call • Stop early if index exceeds word length or characters mismatch. 3️⃣ Find the Duplicate Number Problem: Find the single duplicate in an array of size n+1 Efficient Approach (Index Marking): • For each element x: • Go to index abs(x) • If value at that index is already negative → duplicate found • Else, mark it negative My solution links- https://lnkd.in/gf4b-E6K https://lnkd.in/gNv9gtgB https://lnkd.in/gGatDNSK 📌 Consistency > Motivation. Building logic daily, one problem at a time. #100DaysOfCode #Day3 #Java #DSA #ProblemSolving #LeetCode #BackendDevelopment #LearningInPublic #SoftwareEngineer #Consistency #PlacementPrep
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