🌿 Day 74 of #100DaysOfCode 🌿 💡 Problem: Binary Tree Preorder Traversal – LeetCode 🚀 Approach: Used a recursive traversal to explore nodes in the order Root → Left → Right. Preorder traversal is all about visiting the leader first — just like taking the initiative before exploring possibilities! 💫 📊 Complexity Analysis: Time Complexity: O(n)** — every node is visited once Space Complexity: O(n)** — due to recursion stack ✅ Runtime: 0 ms (⚡ Beats 100%) ✅ Memory: 43.06 MB 🔑 Key Insight: Recursion helps untangle even the deepest branches — one root call at a time 🌱 #LeetCode #100DaysOfCode #BinaryTree #Recursion #CodingJourney #DSA #ProblemSolving #Java #Algorithms #ProgrammerLife #WomenInTech #CodeEveryday
Solved Binary Tree Preorder Traversal with recursion on LeetCode
More Relevant Posts
-
Day 41 — DSA From Scratch Today's problem: Binary Tree Inorder Traversal The task was to return the inorder traversal of a binary tree — visiting nodes in the order Left → Root → Right. Approach Used: Used a simple recursive approach — Traverse the left subtree. Visit and record the current node’s value. Traverse the right subtree. This approach naturally follows the definition of inorder traversal and helps in understanding tree recursion better. Time Complexity: O(n) — each node is visited once. Space Complexity: O(h) — where h is the height of the tree (recursion stack). Another small step in getting comfortable with recursion and binary tree fundamentals. #Day41 #DSAFromScratch #100DaysOfCode #BinaryTrees #LeetCode #ProblemSolving #Java #Recursion #DataStructures #LearningEveryday #Consistency
To view or add a comment, sign in
-
-
📅 Day 81 of #100DaysOfLeetCode Problem: Insert into a Binary Search Tree (LeetCode #701) Approach: The task is to insert a new node with a given value into a Binary Search Tree (BST). Start from the root and recursively find the correct position: If the new value is smaller than the current node’s value, go to the left subtree. Otherwise, go to the right subtree. When a null spot is found, insert a new node there. The BST property is preserved throughout this process. Complexity: ⏱️ Time: O(h) — where h is the height of the tree. 💾 Space: O(h) — recursive call stack. 🔗 Problem Link: https://lnkd.in/dCS7zxVG 🔗 Solution Link: https://lnkd.in/dxB4ZNtV #LeetCode #100DaysOfCode #BinarySearchTree #Recursion #Java #TreeTraversal #DSA #Algorithms #CodingChallenge #ProblemSolving #CodeNewbie #StudyWithMe #BuildInPublic #LearnToCode #DailyCoding
To view or add a comment, sign in
-
-
🌳 Day 73 of #100DaysOfCode 🌳 💡 Problem: Binary Tree Inorder Traversal – LeetCode ✨ Approach: Implemented a recursive inorder traversal to explore the binary tree in Left → Root → Right order. A perfect balance of recursion and logic — the beauty of tree traversal lies in its simplicity 🌿 📊 Complexity Analysis: Time Complexity: O(n)** — visiting each node exactly once Space Complexity: O(n)** — due to recursion stack and list storage ✅ Runtime: 0 ms (🔥 Beats 100%) ✅ Memory: 43.25 MB 🔑 Key Insight: Recursion simplifies complex tree structures into elegant solutions — one node at a time 🌱 #LeetCode #100DaysOfCode #DataStructures #BinaryTree #Recursion #ProblemSolving #DSA #AlgorithmDesign #CodeEveryday #ProgrammerJourney #Java #CodingChallenge
To view or add a comment, sign in
-
-
💡 LeetCode #167 — Two Sum II (Input Array Is Sorted) Today I solved LeetCode Problem 167: Two Sum II — Input Array Is Sorted 🎯 Problem Summary: You are given a sorted array and a target. Return the 1-indexed positions of the two numbers that add up to the target. You must use O(1) extra space. Key Idea: Use the two-pointer technique 👈👉 Start with one pointer at the beginning (left) and one at the end (right). If the sum is too large → move right leftward. If the sum is too small → move left rightward. Stop when you find the pair. This works because the array is already sorted. #LeetCode #Java #DSA #TwoPointers #Algorithms #ProblemSolving #CodingChallenge
To view or add a comment, sign in
-
-
✅Day 44 : Leetcode 162 - Find Peak Element #60DayOfLeetcodeChallenge 🧩 Problem Statement: You are given a 0-indexed integer array nums. A peak element is an element that is strictly greater than its neighbors. Your task is to find a peak element and return its index. If the array contains multiple peaks, return the index of any one of them. You must solve this in O(log n) time complexity. 💡 My Approach: I used the Binary Search approach to solve this problem efficiently. Check for edge cases: If there’s only one element, return index 0. If the first element is greater than the second, it’s a peak → return 0. If the last element is greater than the second last, return n-1. Apply binary search between indices 1 and n-2: Find the middle index mid. If nums[mid] is greater than both its neighbors (nums[mid-1] and nums[mid+1]), we found the peak → return mid. If the left neighbor is greater, move the search to the left half. Otherwise, move to the right half. This guarantees logarithmic search efficiency. ⏱️ Time Complexity: O(log n) — due to binary search. 💾 Space Complexity: O(1) — constant extra space. #BinarySearch #LeetCode #FindPeakElement #DSA #Java #CodingPractice #ProblemSolving #LogN
To view or add a comment, sign in
-
-
#98day of #100DaysOfCode 🌿 LeetCode 1302: Deepest Leaves Sum Today’s problem was about exploring the depths of a binary tree 🌳 We needed to find the sum of all the deepest leaf nodes — those that rest at the very bottom of the tree. 🧠 Approach: Two main ways to solve it: 1️⃣ Level Order Traversal (BFS): Traverse level by level; the last level’s sum is the answer. 2️⃣ Depth-first Search (DFS): Track maximum depth and accumulate the sum of deepest nodes. 📈 Complexity: Time: O(n) Space: O(n) 💬 Learning: Whether in trees or life, depth reveals value — the deeper you go, the richer the sum 🌱 #LeetCode #DSA #BinaryTree #CodingChallenge #java #ProblemSolving #LeetCodeDaily #100DaysOfCode #Algorithm
To view or add a comment, sign in
-
-
💻 Day 19 of My LeetCode Journey 🧩 Problem: 205. Isomorphic String ✨ My Approach: To check if two strings are isomorphic, I used two HashMaps — one to store character mappings from string s to string t, and another to track whether a character in t has already been mapped. 🔹 If a mapping already exists, I verify that it’s consistent. 🔹 If not, I ensure no two characters from s map to the same character in t. 🔹 If all mappings hold correctly, the strings are isomorphic. ⚙️ Time Complexity: O(n) 💾 Space Complexity: O(n) ✅ Learning: This problem helped me understand the concept of one-to-one mapping between characters and how to use HashMap effectively to track relationships between elements. #Day19 #LeetCode #Java #CodingJourney #ProblemSolving #IsomorphicStrings #HashMap #LearningEveryday #DataStructures
To view or add a comment, sign in
-
-
Day 38 — DSA From Scratch (Easy) Today's problem: Same Tree The task was to check if two binary trees are structurally identical and have the same node values. Approach Used: Used a recursive approach — If both nodes are null, they are the same. If both are non-null and values match, recursively check their left and right subtrees. If any mismatch occurs, return false immediately. Time Complexity: O(n) — each node in both trees is visited once. Space Complexity: O(h) — where h is the height of the tree (due to the recursion stack). Learning more about recursion and binary tree structures each day.Little steps, steady progress. #Day38 #DSAFromScratch #100DaysOfCode #BinaryTrees #LeetCode #ProblemSolving #Java #Recursion #DataStructures #LearningEveryday #Consistency
To view or add a comment, sign in
-
-
Day 39 — DSA From Scratch Today's problem: Subtree of Another Tree The task was to check whether one binary tree is a subtree of another. Approach Used: Used a recursive approach — If the current node in the main tree matches the root of the subtree, check if both trees are identical using a helper function. If not, recursively check the left and right subtrees. The helper function verifies if two trees have the same structure and values. Time Complexity: O(m × n) — in the worst case, we compare each node of one tree with every node of the other. Space Complexity: O(h) — where h is the height of the main tree (due to recursion). Every new problem adds a small piece to the puzzle. Learning how recursion ties everything together in binary trees. #Day39 #DSAFromScratch #100DaysOfCode #BinaryTrees #LeetCode #ProblemSolving #Java #Recursion #DataStructures #LearningEveryday #Consistency
To view or add a comment, sign in
-
-
#Day_31 Today’s challenge was an interesting matrix binary search problem — “Find a Peak Element II”-> it's a medium level question. The task: Given a 2D grid, find any element that’s strictly greater than its top, bottom, left, and right neighbors. A brute-force solution would check every element’s neighbors — but that’s O(m × n). Instead, I used a binary search on columns to cut the search space efficiently. Here’s the idea: Pick the middle column. Find the maximum element in that column. Compare it with its left and right neighbors. If it’s greater than both — you’ve found a peak. Otherwise, move to the side that has a larger neighbor (since a peak must exist there). This clever approach brings the complexity down to O(m × log n) — a huge win on large matrices. This problem was a great reminder that binary search isn’t just for 1D arrays — it can be applied creatively in multiple dimensions too #Coding #LeetCode #Java #BinarySearch #ProblemSolving #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