Day 91 of #100DaysOfCode Today I solved "Insert into a Binary Search Tree" on LeetCode using a Recursive approach. Key Idea: In a Binary Search Tree (BST): • Left subtree → values less than root • Right subtree → values greater than root So we just follow the correct path until we find the right position to insert the new value. Approach: • If root is null → create and return new node • If value > root → go to right subtree • Else → go to left subtree • Recursively insert until correct position is found Concepts Used: • Binary Search Tree (BST) • Recursion • Tree traversal Time Complexity: O(h) Space Complexity: O(h) This problem reinforces the importance of BST properties for efficient insertion Simple logic, powerful structure #Day91 #100DaysOfCode #LeetCode #BST #Recursion #Cpp #CodingJourney
Insert into Binary Search Tree using Recursion on LeetCode
More Relevant Posts
-
🚀 #100DaysOfCode Day 77/100 – Permutations 🧠 Problem: Given an array of distinct integers, return all possible permutations. 👉 Order matters here → [1,2,3] ≠ [3,2,1] 💡 Core Idea This is a classic Backtracking + Swapping problem 🔥 1️⃣ Fix one element at current index 2️⃣ Swap it with every possible element 3️⃣ Recursively generate permutations for remaining 4️⃣ Backtrack (swap back) 👉 Swap → Recurse → Undo 📚 Key Learnings 1. Backtracking with swapping technique 2. Generating all permutations efficiently 3. Understanding recursion tree deeply ⏱️ Complexity Time Complexity: O(n!) Space Complexity: O(n) (recursion stack) #100DaysOfCode #Day77 #LeetCode #DSA #Backtracking #Recursion #Algorithms #CodingJourney #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 46 Today I solved: Binary Search (LeetCode 704) 💡 Problem: Given a sorted array, find the index of a target element. If it doesn’t exist, return -1. 💡 My Approach: I used the classic Binary Search technique: 1️⃣ Initialize two pointers: start and end 2️⃣ Find middle index mid 3️⃣ Compare nums[mid] with target 👉 If equal → return mid 👉 If target is greater → search right half 👉 If target is smaller → search left half 4️⃣ Repeat until found or search space is exhausted 💡 Key Insight: Instead of checking every element ❌ Divide the search space in half each time ✅ ⚡ Complexity: Time: O(log n) Space: O(1) #LeetCode #DSA #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
Day 74/150 🚀 LeetCode 226: Invert Binary Tree 🧠 Problem Given the root of a binary tree, invert the tree and return its root. 📌 Example Input → root = [4,2,7,1,3,6,9] Output → [4,7,2,9,6,3,1] 💡 Approach • Use recursion • Swap left and right child • Recursively invert left subtree • Recursively invert right subtree ⏱ Time Complexity O(n) 📦 Space Complexity O(h) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day74 #LeetCode #DSA #BinaryTree #Recursion #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 84/150 🚀 LeetCode 124: Binary Tree Maximum Path Sum 🧠 Problem Find the maximum path sum in a binary tree. A path can start and end at any node. 💡 Approach • Use DFS (post-order traversal) • Ignore negative paths → max(0, left/right) • Update global max using left + right + root • Return max single path for recursion ⏱ Time Complexity O(n) 📦 Space Complexity O(h) (recursion stack) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) Hard problems getting comfortable 🌳 #Day84 #LeetCode #BinaryTree #DFS #Recursion #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 167/365 – DSA Challenge 🚀 Solved Construct Binary Tree from Preorder and Inorder Traversal on LeetCode today. 🔹 Problem: Given two arrays — preorder and inorder traversal of a binary tree — construct the original tree. 🔹 Approach Used: Recursion + HashMap Steps followed: 1️⃣ First element of preorder → root 2️⃣ Find root in inorder using a hashmap (for O(1) lookup) 3️⃣ Split inorder into: Left subtree Right subtree 4️⃣ Recursively build left and right subtrees 🔹 Key Idea: Preorder gives the root, inorder gives the structure 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(n) 💻 Language: C++ This is a classic problem that tests your understanding of tree reconstruction + recursion depth control. #Day167 #365DaysOfCode #DSA #LeetCode #BinaryTree #Recursion #HashMap #CodingChallenge #Cpp
To view or add a comment, sign in
-
-
Day 90 of #100DaysOfCode Today I solved "Path Sum III" on LeetCode using a DFS + Recursion approach. Key Idea: We need to count all paths where the sum of node values equals the target. The path doesn’t have to start from the root — it can start from any node! Approach: • For every node, treat it as a starting point • Use DFS to explore all downward paths • Reduce the target at each step (target - node->val) • If a node matches the remaining target → count it • Repeat the process for left and right subtrees Why this works: Every node gets a chance to act as the starting point, and DFS ensures we explore all possible paths efficiently. Concepts Used: • Binary Trees • Depth First Search (DFS) • Recursion • Backtracking (implicit) Time Complexity: O(n²) in worst case Space Complexity: O(h) This problem helped me understand how to explore all possible paths in a tree, not just root-based ones — a big step forward in mastering tree problems From single path problems → to handling multiple dynamic paths… growing every day #Day90 #100DaysOfCode #LeetCode #BinaryTree #DFS #Cpp #CodingJourney
To view or add a comment, sign in
-
-
-- Solved LeetCode 100: Same Tree -- Today I worked on the classic binary tree problem “Same Tree”, and it turned out to be a great exercise in strengthening recursion fundamentals -- Problem Summary: -- Given two binary trees, determine whether they are identical in both structure and node values. -- Approach (Recursive DFS): At each node, I focused on 3 key checks: • If both nodes are NULL → trees match at this point • If one is NULL → structure mismatch • If values differ → not the same If all checks pass, recursively compare left and right subtrees. -- Complexity: Time: O(n) – visiting every node once Space: O(h) – recursion stack (depends on tree height) Consistency > Complexity. #LeetCode #DSA #Recursion #BinaryTree #CodingJourney #SoftwareEngineering #algorithm
To view or add a comment, sign in
-
-
Day 97 of #100DaysOfCode Today I solved "Maximum Width of Binary Tree" on LeetCode using Level Order Traversal (BFS) + Indexing. Key Idea: Treat the binary tree like a complete binary tree by assigning indices to nodes: • Left child → 2 * index • Right child → 2 * index + 1 This helps us calculate the width of each level easily. Approach: • Use a queue storing {node, index} • For each level: Take first and last index Width = right - left + 1 • Normalize indices to avoid overflow Concepts Used: • Binary Trees • Breadth First Search (BFS) • Queue • Index-based traversal Time Complexity: O(n) Space Complexity: O(n) This problem was interesting because it combines tree traversal with indexing logic to solve a non-trivial problem From simple traversal → to smart indexing tricks #Day97 #100DaysOfCode #LeetCode #BinaryTree #BFS #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 2️⃣ /15 — Consistency Challenge 🚀 Today’s problem: LeetCode 3488 Closest Equal Element Queries 🔹 Approach: The idea is to efficiently find the nearest same element for each query in a circular array. Using Hashmap For each query q: Find the value → nums[q] Get the list of indices where this value occurs. Edge Case: If the value appears only once → answer is -1. Binary Search: Using Collections.binarySearch to find the position of index q in the list. This allows us to locate the nearest neighbors efficiently. Check Neighbors (Circular): Left neighbor → (pos - 1 + size) % size Right neighbor → (pos + 1) % size These represent the closest occurrences of the same value. Distance Calculation: Since the array is circular: Direct distance → |i - j| Circular distance → n - |i - j| Take the minimum of both. Final Answer: Return the minimum distance among left and right neighbors. 🔹 Time Complexity: O(n + q log k) 👉 Efficient because we avoid scanning the whole array for every query. Still improving. One day at a time. 💯 #DSA #LeetCode #Consistency #DayChallenge #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
🎯Day 17 of #150DaysOfCode 🚀 👨💻 Platform: #LeetCode 🧠 Today’s Problem: ⚡ 17. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: strs = ["flower","flow","flight"] Output: "fl" Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Constraints: 1 <= strs.length <= 200 0 <= strs[i].length <= 200 strs[i] consists of only lowercase English letters if it is non-empty.
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