question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
binary-tree-preorder-traversal
javascript stack 98% runtime, step by step
javascript-stack-98-runtime-step-by-step-w8rj
For anyone (new to algo/ds) who\'s beaten by this, here\'s an step by step explanation:\n\n\ntree:\n 1\n 2 3\n4 5 6 7\n\n1st iteration:\nstack: [1]\n\n2
seanwth
NORMAL
2020-02-25T17:11:42.947739+00:00
2020-02-25T17:11:42.947778+00:00
1,840
false
For anyone (new to algo/ds) who\'s beaten by this, here\'s an step by step explanation:\n\n```\ntree:\n 1\n 2 3\n4 5 6 7\n\n1st iteration:\nstack: [1]\n\n2nd iteration:\ncurrent node = stack pop last element = 1\ncurrent node:\n 1\n 2 3\nstack push right of current node first then followed by left \nstack: ...
21
0
['Stack', 'Iterator', 'JavaScript']
7
binary-tree-preorder-traversal
Easy || 0 ms || 100% || Fully Explained (Java, C++, Python, Python3) || Recursive || Iterative
easy-0-ms-100-fully-explained-java-c-pyt-8dhu
Java Solution (Iterative Approach Using Stack):\nRuntime: 1 ms, faster than 93.68% of Java online submissions for Binary Tree Preorder Traversal.\nMemory Usage:
PratikSen07
NORMAL
2022-08-18T08:51:35.908179+00:00
2022-08-18T08:54:51.016403+00:00
1,855
false
# **Java Solution (Iterative Approach Using Stack):**\nRuntime: 1 ms, faster than 93.68% of Java online submissions for Binary Tree Preorder Traversal.\nMemory Usage: 41.3 MB, less than 84.60% of Java online submissions for Binary Tree Preorder Traversal.\n```\nclass Solution {\n public List<Integer> preorderTravers...
17
0
['Stack', 'Recursion', 'C', 'Python', 'Java', 'Python3']
0
binary-tree-preorder-traversal
Java || 2 Easy Approach with Explanation || DSF and Stack
java-2-easy-approach-with-explanation-ds-m7yo
\n1)\n//Recursive Solution //DFS\nclass Solution \n{\n List<Integer> res= new ArrayList<>();//global ArrayList \n\n public List<Integer> preorderTraversal
swapnilGhosh
NORMAL
2021-07-07T06:32:47.086605+00:00
2021-07-07T06:32:47.086645+00:00
903
false
```\n1)\n//Recursive Solution //DFS\nclass Solution \n{\n List<Integer> res= new ArrayList<>();//global ArrayList \n\n public List<Integer> preorderTraversal(TreeNode root)\n {\n if(root==null)//base case for the null graph \n return res;\n\n preorder(root);//calling the preorder in...
17
0
['Stack', 'Recursion', 'Binary Tree', 'Iterator', 'Java']
1
binary-tree-preorder-traversal
Python solution
python-solution-by-zitaowang-cla6
Recursive:\n\nclass Solution(object):\n def preorderTraversal(self, root):\n """\n :type root: TreeNode\n :rtype: List[int]\n """
zitaowang
NORMAL
2018-08-28T07:06:10.499406+00:00
2018-09-17T15:28:34.526380+00:00
2,172
false
Recursive:\n```\nclass Solution(object):\n def preorderTraversal(self, root):\n """\n :type root: TreeNode\n :rtype: List[int]\n """\n if not root:\n return []\n elif not root.left and not root.right:\n return [root.val]\n l = self.preorderTraver...
17
0
[]
1
binary-tree-preorder-traversal
Share my solution in C
share-my-solution-in-c-by-milu-lms9
//// iterative solution\n\n int preorderTraversal(struct TreeNode root, int returnSize) {\n int result = NULL;\n if (root == NULL)\n return resu
milu
NORMAL
2015-10-22T03:35:06+00:00
2015-10-22T03:35:06+00:00
2,260
false
//// iterative solution\n\n int* preorderTraversal(struct TreeNode* root, int* returnSize) {\n int *result = NULL;\n if (root == NULL)\n return result;\n \n *returnSize = 0;\n \n struct TreeNode **stack = (struct TreeNode **)malloc(sizeof(struct TreeNode *));\n struct TreeNode *pop;\n...
17
0
[]
2
binary-tree-preorder-traversal
Proper Tree Traversal || DFS || Python Easy || Begineer Friendly ||
proper-tree-traversal-dfs-python-easy-be-p9co
Intuition\n Describe your first thoughts on how to solve this problem. \n- The intuition here is to perform a preorder traversal of a binary tree and collect th
Pratikk_Rathod
NORMAL
2023-09-28T07:48:04.439450+00:00
2023-09-28T07:48:04.439499+00:00
1,606
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The intuition here is to perform a preorder traversal of a binary tree and collect the values of the nodes in a list as you traverse the tree.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- We can implement a ...
16
0
['Stack', 'Tree', 'Depth-First Search', 'Binary Tree', 'Python', 'Python3']
0
binary-tree-preorder-traversal
Python Preorder/Inorder/Postorder Iterative and Recursive Summary
python-preorderinorderpostorder-iterativ-l3nn
Preorder:\n144. Binary Tree Preorder Traversal\n> Time Complexity O(N)\n> Space Complexity O(1)\n\n## Recursively\ndef preorderTraversal1(self, root):\n res
letscoding_john
NORMAL
2019-09-06T16:06:22.421179+00:00
2019-09-06T16:10:11.670119+00:00
2,602
false
Preorder:\n**144. Binary Tree Preorder Traversal**\n> Time Complexity O(N)\n> Space Complexity O(1)\n```\n## Recursively\ndef preorderTraversal1(self, root):\n res = []\n self.dfs(root, res)\n return res\n \ndef dfs(self, root, res):\n if root:\n res.append(root.val)\n self.dfs(root.left, r...
16
3
['Tree']
0
binary-tree-preorder-traversal
✔️ 100% Fastest Swift Solution
100-fastest-swift-solution-by-sergeylesc-dnrx
\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public var val: Int\n * public var left: TreeNode?\n * public var right
sergeyleschev
NORMAL
2022-04-13T05:46:47.115640+00:00
2022-04-13T05:46:47.115688+00:00
476
false
```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public var val: Int\n * public var left: TreeNode?\n * public var right: TreeNode?\n * public init() { self.val = 0; self.left = nil; self.right = nil; }\n * public init(_ val: Int) { self.val = val; self.left = nil; sel...
15
0
['Swift']
2
binary-tree-preorder-traversal
[C++] 100% Time - DFS Recursive vs. Iterative Solutions Explained and Compared
c-100-time-dfs-recursive-vs-iterative-so-vstq
First in the easy, convenient way of a plain DFS: we just move left as much as possible, update res at each step and only then we have met the last left branch
ajna
NORMAL
2020-07-24T18:48:50.521394+00:00
2020-07-24T18:48:50.521423+00:00
1,122
false
First in the easy, convenient way of a plain DFS: we just move `left` as much as possible, update `res` at each step and only then we have met the last `left` branch on our current path we start considering going to the `right`:\n\n```cpp\nclass Solution {\npublic:\n vector<int> res;\n void dfs(TreeNode *root) {\...
15
0
['Depth-First Search', 'Recursion', 'C', 'Iterator', 'C++']
3
binary-tree-preorder-traversal
Easiest Traversal Without Stack & Queue || 100% || Preorder.
easiest-traversal-without-stack-queue-10-mxp0
Using This Solution We Can Make Preorder Traversal In Binary Tree Without Using Stack And Queue.\n\n##### Global Declaration Of Ans Vector.\n\nclass Solution {\
Bluster980
NORMAL
2022-08-06T06:37:02.343859+00:00
2023-07-01T18:49:37.264320+00:00
135
false
## **Using This Solution We Can Make Preorder Traversal In Binary Tree Without Using Stack And Queue.**\n\n##### Global Declaration Of Ans Vector.\n```\nclass Solution {\npublic:\n vector<int> v;\n vector<int> preorderTraversal(TreeNode* root) {\n if(root == NULL){\n return v;\n }\n ...
14
0
['Tree', 'Binary Search Tree']
2
binary-tree-preorder-traversal
Python | Recursive & Iterative | Simple Solutions
python-recursive-iterative-simple-soluti-172t
Recursive Solution\n\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val
akash3anup
NORMAL
2021-10-10T18:47:27.880502+00:00
2021-10-10T18:47:27.880534+00:00
1,134
false
### Recursive Solution\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def traversal(self, root, preorder):\n if root:\n preo...
14
0
['Python']
0
binary-tree-preorder-traversal
🔥 [LeetCode The Hard Way]🔥 DFS | Pre Order | Explained Line By Line
leetcode-the-hard-way-dfs-pre-order-expl-1pvc
\uD83D\uDD34 Check out LeetCode The Hard Way for more solution explanations and tutorials. \n\uD83D\uDFE0 Check out our Discord for live discussion.\n\uD83D\uDF
__wkw__
NORMAL
2022-09-08T11:37:01.576131+00:00
2023-01-09T02:34:59.930618+00:00
1,971
false
\uD83D\uDD34 Check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. \n\uD83D\uDFE0 Check out our [Discord](https://discord.gg/Nqm4jJcyBf) for live discussion.\n\uD83D\uDFE2 Give a star on [Github Repository](https://github.com/wingkwong/leetco...
13
1
['Recursion', 'C', 'Python', 'C++', 'Python3']
0
binary-tree-preorder-traversal
[JavaScript] Simple iterative solution
javascript-simple-iterative-solution-by-rkn42
\nvar preorderTraversal = function(root) {\n const result = [];\n const stack = [];\n let node = root;\n \n while (node) {\n node.val && result.push(nod
pdxandi
NORMAL
2018-10-25T23:14:00.058047+00:00
2018-10-25T23:14:00.058106+00:00
1,003
false
```\nvar preorderTraversal = function(root) {\n const result = [];\n const stack = [];\n let node = root;\n \n while (node) {\n node.val && result.push(node.val); \n node.right && stack.push(node.right); \n node = node.left || stack.pop(); \n }\n \n return result;\n};\n```
13
0
[]
2
binary-tree-preorder-traversal
Easy to read JAVA solutions for both iterative and recursive (<300ms)
easy-to-read-java-solutions-for-both-ite-g5be
Recursive:\n\n public class Solution {\n List traversal = new ArrayList<>();\n public List preorderTraversal(TreeNode root) {\n if(r
2010zhouyang
NORMAL
2015-08-03T21:27:56+00:00
2015-08-03T21:27:56+00:00
1,858
false
Recursive:\n\n public class Solution {\n List<Integer> traversal = new ArrayList<>();\n public List<Integer> preorderTraversal(TreeNode root) {\n if(root!=null){helper(root);}\n return traversal;\n }\n \n void helper (TreeNode root){\n traversal.add...
13
0
[]
1
binary-tree-preorder-traversal
Video solution | Template sol. for all traversals (Preorder, Inorder, Postorder) | With Examples
video-solution-template-sol-for-all-trav-hulo
Video\nHey everyone i have created a video solution for all DFS traversals (Preorder, Inorder, Postorder) for binary trees, it has a template solution which wil
_code_concepts_
NORMAL
2024-07-09T09:50:06.104755+00:00
2024-07-12T10:20:34.013717+00:00
1,246
false
# Video\nHey everyone i have created a video solution for all DFS traversals (Preorder, Inorder, Postorder) for binary trees, it has a template solution which will help you to implement all the travesals in one go, i have also tried to explain each travesal with a practical example, so that you understand why these tra...
12
0
['C++']
1
binary-tree-preorder-traversal
Java easy solution 0ms 100% faster || Recursion
java-easy-solution-0ms-100-faster-recurs-cz2s
Code\n\njava\npublic List<Integer> preorderTraversal(TreeNode root) {\n\tList<Integer> list = new ArrayList<>();\n\tpreorder(root, list);\n\treturn list;\n}\n\n
Chaitanya31612
NORMAL
2021-11-20T15:29:18.377419+00:00
2021-11-20T15:29:18.377447+00:00
609
false
**Code**\n\n```java\npublic List<Integer> preorderTraversal(TreeNode root) {\n\tList<Integer> list = new ArrayList<>();\n\tpreorder(root, list);\n\treturn list;\n}\n\npublic void preorder(TreeNode root, List<Integer> list) {\n\tif(root == null) return;\n\n\tlist.add(root.val);\n\tpreorder(root.left, list);\n\tpreorder(...
12
1
['Recursion', 'Java']
0
binary-tree-preorder-traversal
Simple recursive JavaScript solution
simple-recursive-javascript-solution-by-wugcr
\nvar preorderTraversal = function solution(root) {\n if (!root) {\n return [];\n }\n \n return [root.val, ...solution(root.left), ...solutio
nnigmat
NORMAL
2021-01-08T12:36:21.685940+00:00
2021-01-08T12:36:53.369030+00:00
703
false
```\nvar preorderTraversal = function solution(root) {\n if (!root) {\n return [];\n }\n \n return [root.val, ...solution(root.left), ...solution(root.right)];\n};\n```
12
0
['Recursion', 'JavaScript']
1
binary-tree-preorder-traversal
Iterative and Recursive DFS JS Solutions
iterative-and-recursive-dfs-js-solutions-bnf6
\n// Iterative DFS Solution\nvar preorderTraversal = function(root) {\n if (!root) return [];\n let stack = [], res = [];\n stack.push(root);\n whil
hbjorbj
NORMAL
2020-10-05T20:11:58.889330+00:00
2021-06-25T15:10:21.700915+00:00
1,289
false
```\n// Iterative DFS Solution\nvar preorderTraversal = function(root) {\n if (!root) return [];\n let stack = [], res = [];\n stack.push(root);\n while (stack.length) {\n let node = stack.pop();\n res.push(node.val);\n if (node.right) stack.push(node.right);\n if (node.left) sta...
12
0
['JavaScript']
5
binary-tree-preorder-traversal
Easy , Beginner friendly & Dry run ||Pre Order Traversal || Time O(n) & Space O(n) || Gits✅✅😎😎
easy-beginner-friendly-dry-run-pre-order-kqiv
Guys if the solution and explanation is helpful the upvote me.\u2764\uFE0F\u2764\uFE0F\n\n\n\n# Approach \uD83D\uDC48\n\n- In the question given, the root of a
GiteshSK_12
NORMAL
2024-02-05T07:20:14.917769+00:00
2024-02-05T07:20:14.917800+00:00
999
false
# Guys if the solution and explanation is helpful the upvote me.\u2764\uFE0F\u2764\uFE0F\n\n![Screenshot 2024-02-05 114830.png](https://assets.leetcode.com/users/images/265d4aa0-efe4-458b-90b9-71a57153caac_1707117581.8208427.png)\n\n# Approach \uD83D\uDC48\n\n- In the question given, the root of a binary tree is provid...
10
0
['Array', 'Tree', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3', 'Ruby', 'JavaScript', 'C#']
2
binary-tree-preorder-traversal
C++ || Recursion || Full Explanation
c-recursion-full-explanation-by-nirdeshd-ub5g
Intuition\n Describe your first thoughts on how to solve this problem. \n- Using Recursive Approach.\n\n# Approach\n- A binary tree is a recursive object where
nirdeshdevadiya17
NORMAL
2023-01-09T02:43:18.876217+00:00
2023-01-09T02:43:18.876257+00:00
116
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Using Recursive Approach.\n\n# Approach\n- A binary tree is a recursive object where we have three essential components\n - Root node: The topmost node of a binary tree.\n\n - Left-subtree: A smaller binary tree connected via the...
10
0
['Tree', 'Recursion', 'C++']
0
binary-tree-preorder-traversal
Three ways of Iterative Preorder Traversing. Easy Explanation
three-ways-of-iterative-preorder-travers-l2ip
Three types of Iterative Preorder Traversals in java. \n\n1) Using 1 Stack. O(n) Time & O(n) Space\n\t Print and push all left nodes into the stack till it hits
xruzty
NORMAL
2016-10-24T18:35:22.076000+00:00
2016-10-24T18:35:22.076000+00:00
2,233
false
Three types of Iterative Preorder Traversals in java. \n\n1) **Using 1 Stack.** O(n) Time & O(n) Space\n\t* Print and push all `left` nodes into the `stack` till it hits `NULL`.\n\t* Then `Pop` the top element from the stack, and make the `root` point to its `right`.\n\t* Keep iterating till `both` the below conditions...
10
0
[]
1
binary-tree-preorder-traversal
Morris Traversal In Order, pre Order, Post Order
morris-traversal-in-order-pre-order-post-seo5
\n# Code\njava []\npublic class Solution {\n public List<Integer> preorderTraversal(TreeNode root) {\n List<Integer> preorder = new ArrayList<>();\n
Dixon_N
NORMAL
2024-09-04T20:42:57.863078+00:00
2024-09-04T20:42:57.863111+00:00
792
false
\n# Code\n```java []\npublic class Solution {\n public List<Integer> preorderTraversal(TreeNode root) {\n List<Integer> preorder = new ArrayList<>();\n TreeNode cur = root;\n \n while (cur != null) {\n if (cur.left == null) {\n preorder.add(cur.val);\n ...
9
0
['Tree', 'Java']
5
binary-tree-preorder-traversal
Python 3 (30ms) | Perfect Pythonic Recursive One-Liner | Easy to Understand
python-3-30ms-perfect-pythonic-recursive-useb
\nclass Solution:\n def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n if root is None:\n return []\n return [roo
MrShobhit
NORMAL
2022-01-27T15:08:16.960883+00:00
2022-01-27T15:08:16.960916+00:00
1,225
false
```\nclass Solution:\n def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n if root is None:\n return []\n return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)\n\n```
9
0
['Recursion', 'Binary Tree', 'Python', 'Python3']
2
binary-tree-preorder-traversal
C# Recursive and Iterative (using Stack) with explanation
c-recursive-and-iterative-using-stack-wi-s2je
Pre-order traversal is to visit the root first. Then traverse the left subtree. Finally, traverse the right subtree. \n> Preorder: visit > left > right\n\nItera
minaohhh
NORMAL
2021-06-09T08:05:29.551305+00:00
2021-06-09T08:08:29.994478+00:00
1,260
false
Pre-order traversal is to visit the root first. Then traverse the left subtree. Finally, traverse the right subtree. \n> Preorder: **visit > left > right**\n\n**Iterative Approach**\n* We need to use a `Stack` to remember the sub-trees as we go deeper\n* Since `stack`s are `first-in-last-out` approach, we need to:\n\t*...
9
0
['Stack', 'Recursion', 'Iterator', 'C#']
1
binary-tree-preorder-traversal
Extremely Simple. Morris Traversal. Pre/In/Post Order Summary.
extremely-simple-morris-traversal-preinp-b8ui
There are a lot of introductions about how Morris Traversal works. It is a beautiful algorithm which takes only O(1) space complexity while still maintaining O(
h_cong
NORMAL
2020-12-06T10:01:51.629492+00:00
2020-12-06T10:01:51.629537+00:00
547
false
There are a lot of introductions about how Morris Traversal works. It is a beautiful algorithm which takes only O(1) space complexity while still maintaining O(n) time complexity. Here I summarized how Morris Traversal is implemented w.r.t. all three orders. \n\n**Pre-Order:**\n```\ndef preorderTraversal(self, root: Tr...
9
0
[]
2
binary-tree-preorder-traversal
Simple Iterative Javascript Solution
simple-iterative-javascript-solution-by-pryqw
Of the three main types of binary tree traversals (inorder,preorder,postorder), I find preorder traversal the easiest to implement iteratively. You can just reu
knownfemme
NORMAL
2018-10-03T09:02:30.207634+00:00
2018-10-03T09:02:30.207676+00:00
2,679
false
Of the three main types of binary tree traversals (inorder,preorder,postorder), I find preorder traversal the easiest to implement iteratively. You can just reuse the dfs algorithm, but make sure you push the children onto the stack in such a way that the left child is processed before the right child.\n```\n/**\n * De...
9
0
[]
0
binary-tree-preorder-traversal
Binary Tree PreOrder Traversal - Beats upto 100% of other's runtime
binary-tree-preorder-traversal-beats-upt-3dst
Basic Concept\n Describe your first thoughts on how to solve this problem. \nA PreOrder Traversal is done in the order Root-Left-Right . \n\n---\n\n# Code Walk
ak9807
NORMAL
2024-07-19T18:41:50.275800+00:00
2024-07-20T05:17:40.154579+00:00
1,313
false
# Basic Concept\n<!-- Describe your first thoughts on how to solve this problem. -->\nA PreOrder Traversal is done in the order **Root-Left-Right** . \n\n---\n\n# Code Walk Through\n<!-- Describe your approach to solving the problem. -->\nLet\'s understand the code and approach.\n\n**Initializations**\n1. We initialize...
8
0
['Stack', 'Tree', 'Depth-First Search', 'Binary Tree', 'Java']
0
binary-tree-preorder-traversal
Java || 👍Explained in Detail👍|| Simple & Fast Solution✅|| Recursive & Iterative
java-explained-in-detail-simple-fast-sol-a87y
I do my best everyday to give a clear explanation, so to help everyone improve their skills.\n\nIf you find this helpful, please \uD83D\uDC4D upvote this post a
cheehwatang
NORMAL
2023-01-09T03:12:24.376079+00:00
2023-01-09T03:41:40.464648+00:00
1,222
false
I do my best everyday to give a clear explanation, so to help everyone improve their skills.\n\nIf you find this **helpful**, please \uD83D\uDC4D **upvote** this post and watch my [Github Repository](https://github.com/cheehwatang/leetcode-java).\n\nThank you for reading! \uD83D\uDE04 Comment if you have any questions ...
8
0
['Binary Search', 'Stack', 'Depth-First Search', 'Binary Tree', 'Java']
0
binary-tree-preorder-traversal
Iterative Solution with Stack ✅Beats 100% solution in time✅
iterative-solution-with-stack-beats-100-5x2oi
Intuition\nWe have to do preorder traversal where we print value for root then we go for it\'s left sub tree and in the last we go for right sub tree of the roo
deleted_user
NORMAL
2023-01-09T03:02:31.812109+00:00
2023-01-09T03:02:31.812147+00:00
630
false
# Intuition\nWe have to do preorder traversal where we print value for root then we go for it\'s left sub tree and in the last we go for right sub tree of the root.\n\nstack is the LIFO(Last in first out data structure) so we can use this property\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n...
8
1
['Java']
0
binary-tree-preorder-traversal
Iterative Preorder Travesal using Stack
iterative-preorder-travesal-using-stack-8q6bo
Intuition\nIterative Preorder Travesal using Stack\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity:\nO(n)\n\n
keyuraval
NORMAL
2023-01-02T07:38:52.581296+00:00
2023-01-02T07:39:26.660221+00:00
2,433
false
# Intuition\nIterative Preorder Travesal using Stack\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n *...
8
0
['C++']
3
binary-tree-preorder-traversal
C++ solutions
c-solutions-by-infox_92-gqp6
\nclass Solution {\npublic:\n ListNode* deleteDuplicates(ListNode* head) {\n ListNode* cur_node = head;\n while (cur_node && cur_node->next) {\
Infox_92
NORMAL
2022-11-18T11:49:34.728216+00:00
2022-11-18T11:49:34.728238+00:00
946
false
```\nclass Solution {\npublic:\n ListNode* deleteDuplicates(ListNode* head) {\n ListNode* cur_node = head;\n while (cur_node && cur_node->next) {\n ListNode* next_node = cur_node->next;\n if (cur_node->val == next_node->val)\n cur_node->next = next_node->next;\n ...
8
0
['C', 'C++']
1
binary-tree-preorder-traversal
✅striver's method || Upvote if you like ⬆️⬆️
strivers-method-upvote-if-you-like-by-ra-2ae7
\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNod
ratnesh_maurya
NORMAL
2022-11-12T11:47:42.743484+00:00
2022-12-08T19:06:35.344307+00:00
260
false
\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, Tre...
8
1
['C++']
0
binary-tree-preorder-traversal
C++ solutiona || 0 ms 100% faster
c-solutiona-0-ms-100-faster-by-infox_92-8r3a
\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> preorder;\n stack<TreeNode*> stack;\n if (ro
Infox_92
NORMAL
2022-11-04T14:50:38.913938+00:00
2022-11-04T14:50:38.913970+00:00
1,198
false
```\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> preorder;\n stack<TreeNode*> stack;\n if (root == NULL)\n return preorder;\n stack.push(root);\n while(!stack.empty()) {\n TreeNode* curr = stack.top();\n ...
8
0
['C', 'C++']
0
binary-tree-preorder-traversal
An Iterative 3-in-1 Template for Pre/In/Post-order Traversal
an-iterative-3-in-1-template-for-preinpo-b27j
Explanation\n\nOne only needs to write the code to the corresponding block.\n\nThe concept is exactly how recursion works.\nWe:\n\n- only traverse while the sta
lunluen
NORMAL
2020-02-09T23:53:16.482398+00:00
2020-02-25T10:00:38.475536+00:00
999
false
## Explanation\n\nOne only needs to write the code to the corresponding block.\n\nThe concept is exactly how recursion works.\nWe:\n\n- only traverse while the stack is not empty. (#0)\n- push a left child to the stack right after a pre-order region. (#3)\n- push a right child to the stack right after an in-order reg...
8
0
['Stack', 'Iterator', 'C++']
1
binary-tree-preorder-traversal
Java solution using stack
java-solution-using-stack-by-jeantimex-28yz
public List<Integer> postorderTraversal(TreeNode root) {\n List<Integer> res = new ArrayList<Integer>();\n \n if (root == null)\n return r
jeantimex
NORMAL
2015-07-08T00:28:15+00:00
2015-07-08T00:28:15+00:00
1,193
false
public List<Integer> postorderTraversal(TreeNode root) {\n List<Integer> res = new ArrayList<Integer>();\n \n if (root == null)\n return res;\n \n Stack<TreeNode> stack = new Stack<TreeNode>();\n stack.push(root);\n \n while (!stack.isEmpty()) {\n TreeNode n...
8
0
['Java']
2
binary-tree-preorder-traversal
C# - iterative with Stack
c-iterative-with-stack-by-jdrogin-e6jz
public IList<int> PreorderTraversal(TreeNode root) {\n Stack<TreeNode> stack = new Stack<TreeNode>();\n List<int> list = new List<int>();\n
jdrogin
NORMAL
2016-10-13T16:48:00.272000+00:00
2016-10-13T16:48:00.272000+00:00
834
false
public IList<int> PreorderTraversal(TreeNode root) {\n Stack<TreeNode> stack = new Stack<TreeNode>();\n List<int> list = new List<int>();\n stack.Push(root);\n while (stack.Count() > 0)\n {\n TreeNode current = stack.Pop();\n if (current != null)\n ...
8
0
[]
3
binary-tree-preorder-traversal
144: Solution with step by step explanation
144-solution-with-step-by-step-explanati-t2uj
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nTo traverse a binary tree in a preorder fashion, we start from the root n
Marlen09
NORMAL
2023-02-19T12:27:27.785616+00:00
2023-02-19T12:27:27.785647+00:00
1,175
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTo traverse a binary tree in a preorder fashion, we start from the root node, visit the left subtree, then the right subtree. We will use a stack to keep track of the nodes to be visited. We first push the root node onto the...
7
0
['Tree', 'Depth-First Search', 'Binary Tree', 'Python', 'Python3']
0
binary-tree-preorder-traversal
✔️ 0ms | explained easy solution | C++
0ms-explained-easy-solution-c-by-coding_-07f4
Approach\nIn this question we need to traverse or move through this tree in pre order traversal\n\nFirst the root node, then the next node (left sibling, or lef
coding_menance
NORMAL
2023-01-09T04:41:51.695262+00:00
2023-01-09T04:41:51.695300+00:00
982
false
# Approach\nIn this question we need to traverse or move through this tree in pre order traversal\n\nFirst the root node, then the next node (left sibling, or left node) and after all the left nodes are exhausted on the left subtree, we go back on e node and check the right node or right sibling. Again when all nodes a...
7
0
['C++']
0
binary-tree-preorder-traversal
✅A Classic Approach to Preorder Tree Traversal: A Recursive Solution | Beats 100% Time [C#]✅
a-classic-approach-to-preorder-tree-trav-426g
The PreorderTraversal function is the public interface for the algorithm, and it accepts the root node of a binary tree as an argument. It creates an empty list
shukhratutaboev
NORMAL
2023-01-09T04:27:32.898345+00:00
2023-01-09T04:30:10.441864+00:00
1,638
false
The PreorderTraversal function is the public interface for the algorithm, and it accepts the root node of a binary tree as an argument. It creates an empty list to store the result of the traversal, and then calls the PreorderTraversalRecursive function on the root node to perform the actual traversal.\n\nThe PreorderT...
7
0
['C#']
0
binary-tree-preorder-traversal
DAILY LEETCODE SOLUTION || EASY C++ SOLUTION
daily-leetcode-solution-easy-c-solution-gx8f2
\n//PreorderTraversal Using Recurrsion\nclass Solution {\npublic:\n vector<int> ans;\n void preorder(TreeNode* root)\n {\n if(root==NULL)\n
pankaj_777
NORMAL
2023-01-09T00:04:56.110832+00:00
2023-01-09T12:29:50.355543+00:00
1,930
false
```\n//PreorderTraversal Using Recurrsion\nclass Solution {\npublic:\n vector<int> ans;\n void preorder(TreeNode* root)\n {\n if(root==NULL)\n {\n return ;\n }\n ans.push_back(root->val);\n preorder(root->left);\n preorder(root->right);\n }\n vector<in...
7
0
['Recursion', 'Binary Tree']
1
binary-tree-preorder-traversal
C++ | Inorder / Preorder / Postorder All 3 solution | 0ms faster than 100%
c-inorder-preorder-postorder-all-3-solut-a766
You can observe there is just a minimal diffrence in all 3 ,i.e, it\'s just the way of filling vector while calling fill function. \nInorder Traversal : \n\ncl
vaibhav4859
NORMAL
2021-10-25T13:09:57.943811+00:00
2021-10-25T13:09:57.943842+00:00
638
false
You can observe there is just a minimal diffrence in all 3 ,i.e, it\'s just the way of filling vector while calling fill function. \n***Inorder Traversal :*** \n```\nclass Solution {\npublic:\n vector<int> ans;\n void fill(TreeNode* root){\n if(!root)\n return;\n fill(root->left);\n ...
7
0
['Recursion', 'C', 'C++']
1
binary-tree-preorder-traversal
Three types of solution(recursion, iterative,morris) 100% faster c++
three-types-of-solutionrecursion-iterati-wocg
All Three types of solutions: \n1. Recursion => TC - O(N) and SC - (N)\n2. Iterative => TC - O(N) and SC - (N)\n3. Morris Traversal => TC - O(N) and SC - (1)\n\
akbhobhiya
NORMAL
2021-07-29T06:26:38.057713+00:00
2021-07-29T06:27:44.882910+00:00
507
false
## All Three types of solutions: \n1. Recursion => TC - O(N) and SC - (N)\n2. Iterative => TC - O(N) and SC - (N)\n3. Morris Traversal => TC - O(N) and SC - (1)\n\n#### **Using recursion (4ms time) TC - O(N) and SC - O(N)**\n```\nclass Solution {\npublic:\n vector<int> ans;\n vector<int> preorderTraversal(TreeN...
7
0
['Recursion', 'C', 'Iterator', 'C++']
1
binary-tree-preorder-traversal
C Solution
c-solution-by-robinsgv-h5g2
\n\nint nodeCount(struct TreeNode* root) {\n if (NULL == root)\n return 0;\n \n return (nodeCount(root->left)+ nodeCount(root->right)) + 1;\n}\n
robinsgv
NORMAL
2021-02-06T18:50:14.556142+00:00
2021-02-06T18:50:14.556174+00:00
685
false
```\n\nint nodeCount(struct TreeNode* root) {\n if (NULL == root)\n return 0;\n \n return (nodeCount(root->left)+ nodeCount(root->right)) + 1;\n}\n\nvoid preorder(struct TreeNode* root, int *res, int *size) {\n if(NULL == root)\n return;\n \n res[(*size)++] = root->val;\n preorder(roo...
7
0
[]
2
binary-tree-preorder-traversal
C solution
c-solution-by-gsingh1629-e8wn
void travel(struct TreeNode root,int arr,int size){\n if(root==NULL)\n return;\n arr[(size)++]=root->val;\n travel(root->left,arr,size);\n tr
GSingh1629
NORMAL
2020-10-16T14:17:13.059558+00:00
2020-10-16T14:17:13.059606+00:00
1,216
false
void travel(struct TreeNode *root,int *arr,int *size){\n if(root==NULL)\n return;\n arr[(*size)++]=root->val;\n travel(root->left,arr,size);\n travel(root->right,arr,size);\n}\nint* preorderTraversal(struct TreeNode* root, int* returnSize){\n *returnSize=0;\n int *arr=(int *)malloc(100*sizeof(i...
7
3
['Recursion', 'C']
2
binary-tree-preorder-traversal
javascript
javascript-by-yinchuhui88-b08t
\nvar preorderTraversal = function(root) {\n let result = [];\n dfs(root);\n \n function dfs(root) {\n if(root != null){\n result.
yinchuhui88
NORMAL
2018-10-11T07:01:29.576406+00:00
2018-10-11T07:01:29.576476+00:00
1,473
false
```\nvar preorderTraversal = function(root) {\n let result = [];\n dfs(root);\n \n function dfs(root) {\n if(root != null){\n result.push(root.val);\n dfs(root.left);\n dfs(root.right);\n }\n }\n return result;\n};\n```
7
1
[]
0
binary-tree-preorder-traversal
Simple iterative and recursive solutions
simple-iterative-and-recursive-solutions-7bbf
\nSolution 1, Iterative\n\n def preorderTraversal(self, root):\n stack, ans =[root], []\n while stack:\n node = stack.pop(-1)\n
cmc
NORMAL
2016-01-16T11:11:59+00:00
2016-01-16T11:11:59+00:00
1,511
false
\n**Solution 1, Iterative**\n\n def preorderTraversal(self, root):\n stack, ans =[root], []\n while stack:\n node = stack.pop(-1)\n while node:\n if node.right:\n stack.append(node.right)\n ans.append(node.val)\n node...
7
0
['Python']
1
binary-tree-preorder-traversal
✅💯🔥Simple Code🚀📌|| 🔥✔️Easy to understand🎯 || 🎓🧠Beats 100%🔥|| Beginner friendly💀💯
simple-code-easy-to-understand-beats-100-d1h6
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
atishayj4in
NORMAL
2024-07-25T19:47:03.402668+00:00
2024-08-01T20:04:38.677371+00:00
811
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
6
0
['Stack', 'Tree', 'Depth-First Search', 'C', 'Binary Tree', 'Python', 'C++', 'Java']
1
binary-tree-preorder-traversal
💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 100
easiestfaster-lesser-cpython3javacpython-c72n
Intuition\n\n\n\n\n\n\nJavaScript []\n//JavaScript Code\n/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n * this.val =
Edwards310
NORMAL
2024-04-17T04:12:26.073673+00:00
2024-08-26T08:03:20.396434+00:00
684
false
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/0eb7f58e-0f9a-4134-a55a-fc68e55f7ab9_1713326839.6583364.jpeg)\n![Screenshot 2024-04-17 073521.png](https://assets.leetcode.com/users/images/32853c17-5f6c-47cb-a391-146018580800_1713326849.2931008.png)\n![Screenshot 2024-04-17 074614.png](https:/...
6
0
['Stack', 'Depth-First Search', 'C', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']
2
binary-tree-preorder-traversal
DFS pre order. JS
dfs-pre-order-js-by-hanbi58-0cqb
Approach\nDepth first search.\n\nT: O(n) S: O(n)\n\n\n\n# Code\n\nvar preorderTraversal = function (root, arr = []) {\n DFS(root);\n return arr;\n function
hanbi58
NORMAL
2023-01-09T00:04:33.998498+00:00
2023-01-09T01:03:01.591770+00:00
829
false
# Approach\nDepth first search.\n\nT: O(n) S: O(n)\n\n\n\n# Code\n```\nvar preorderTraversal = function (root, arr = []) {\n DFS(root);\n return arr;\n function DFS(node) {\n if (!node) return;\n arr.push(node.val);\n DFS(node.left);\n DFS(node.right);\n }\n};\n\n```\n\n\nor\n\n# Code\n```\nvar preorde...
6
0
['JavaScript']
0
binary-tree-preorder-traversal
C++ Easy Solution
c-easy-solution-by-hemant_1451-vpnf
\n# Complexity\n- Time complexity : O(N)\n\n- Space complexity : O(1), if we don\u2019t consider the size of the stack for function. Otherwise, O(H) where H is
Hemant_1451
NORMAL
2023-01-06T14:02:47.494920+00:00
2023-01-06T14:02:47.494962+00:00
1,504
false
\n# Complexity\n- Time complexity : $$O(N)$$\n\n- Space complexity : $$O(1)$$, if we don\u2019t consider the size of the stack for function. Otherwise, $$O(H)$$ where H is the height of the tree. \n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n...
6
0
['Tree', 'Binary Tree', 'C++']
1
binary-tree-preorder-traversal
3 Approaches in C++ || Recursive, Iterative, Morris Traversal
3-approaches-in-c-recursive-iterative-mo-ymjl
Recursive Approach:\n\nvoid help(TreeNode* root, vector<int>& ans)\n {\n if(root == NULL)\n return;\n ans.push_back(root->val);\n
diba_lc
NORMAL
2022-07-17T09:00:18.657860+00:00
2022-07-17T09:00:18.657913+00:00
143
false
**Recursive Approach:**\n````\nvoid help(TreeNode* root, vector<int>& ans)\n {\n if(root == NULL)\n return;\n ans.push_back(root->val);\n help(root->left, ans);\n help(root->right, ans);\n }\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> ans;\n ...
6
0
[]
0
binary-tree-preorder-traversal
[JAVA] standard iterative solution
java-standard-iterative-solution-by-juga-p2s8
\nclass Solution {\n public List<Integer> preorderTraversal(TreeNode root) {\n ArrayList<Integer> ans = new ArrayList<Integer>();\n if(root == null)
Jugantar2020
NORMAL
2022-07-06T21:12:39.421661+00:00
2022-07-06T21:12:39.421713+00:00
407
false
```\nclass Solution {\n public List<Integer> preorderTraversal(TreeNode root) {\n ArrayList<Integer> ans = new ArrayList<Integer>();\n if(root == null) return ans;\n \n Stack<TreeNode> st = new Stack<TreeNode>();\n st.push(root);\n \n while(!st.isEmpty()) {\n TreeNode cur = s...
6
0
['Stack', 'Binary Tree', 'Iterator', 'Java']
0
binary-tree-preorder-traversal
100 percent faster || easy to understand
100-percent-faster-easy-to-understand-by-v9tz
class Solution {\n private : vectorres;\n void preorder(TreeNode root, vector&res)\n {\n if(root==NULL)\n return;\n res.push_b
Shivam_sahal
NORMAL
2022-04-23T21:23:56.340138+00:00
2022-04-23T21:23:56.340171+00:00
506
false
class Solution {\n private : vector<int>res;\n void preorder(TreeNode *root, vector<int>&res)\n {\n if(root==NULL)\n return;\n res.push_back(root->val);\n preorder(root->left,res);\n preorder(root->right,res);\n return;\n }\npublic:\n vector<int> preorderTrav...
6
0
['C']
3
binary-tree-preorder-traversal
Simple Python solution (Recursive and Iterative Both)
simple-python-solution-recursive-and-ite-1gdw
Iterative approach using stack:\n\ndef preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n ans=[]\n if root==None:\n retu
thakur_01
NORMAL
2022-03-13T14:57:23.884168+00:00
2022-03-13T14:59:30.229169+00:00
323
false
**Iterative approach using stack:**\n```\ndef preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n ans=[]\n if root==None:\n return None\n stack=[root]\n while len(stack)>0:\n temp=stack.pop(-1)\n ans.append(temp.val)\n if temp.right!=...
6
0
['Binary Tree', 'Python']
0
binary-tree-preorder-traversal
Python3 | easiest solution
python3-easiest-solution-by-anilchouhan1-ktih
\nclass Solution:\n def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n res = []\n \n if root:\n res+= [roo
Anilchouhan181
NORMAL
2022-02-12T13:01:32.505616+00:00
2022-02-12T13:01:49.673791+00:00
505
false
```\nclass Solution:\n def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n res = []\n \n if root:\n res+= [root.val]\n res+= self.preorderTraversal(root.left)\n res+= self.preorderTraversal(root.right)\n return res\n```
6
0
['Python', 'Python3']
1
binary-tree-preorder-traversal
Python 20ms Classic Implementation
python-20ms-classic-implementation-by-ra-4skr
Runtime: 20 ms, faster than 98.55% of Python3 online submissions for Binary Tree Preorder Traversal.\nMemory Usage: 14.1 MB, less than 99.97% of Python3 online
ragav_1302
NORMAL
2020-10-09T17:29:00.960377+00:00
2020-10-09T17:36:44.336539+00:00
476
false
Runtime: 20 ms, faster than 98.55% of Python3 online submissions for Binary Tree Preorder Traversal.\nMemory Usage: 14.1 MB, less than 99.97% of Python3 online submissions for Binary Tree Preorder Traversal.\n\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, righ...
6
0
[]
0
binary-tree-preorder-traversal
👏🏻 Python Stack | DFS - 公瑾™
python-stack-dfs-gong-jin-tm-by-yuzhoujr-9guw
144. Binary Tree Preorder Traversal\n\n> \u7C7B\u578B\uFF1ADFS Stack | Recursive\n> Time Complexity O(n)\n> Space Complexity O(1)\n\n\n#### DFS Recursive\npytho
yuzhoujr
NORMAL
2018-08-09T17:50:46.022872+00:00
2018-10-12T21:10:07.370304+00:00
745
false
### 144. Binary Tree Preorder Traversal\n```\n> \u7C7B\u578B\uFF1ADFS Stack | Recursive\n> Time Complexity O(n)\n> Space Complexity O(1)\n```\n\n#### DFS Recursive\n```python\nclass Solution(object):\n def preorderTraversal(self, root):\n self.res = []\n self.dfs(root)\n return self.res\n \n ...
6
0
[]
2
binary-tree-preorder-traversal
Elegant C++ iterative solution, 0ms, 10 lines
elegant-c-iterative-solution-0ms-10-line-99u7
vector<int> preorderTraversal(TreeNode* root) {\n vector<int> result;\n stack<TreeNode*> q;\n q.push(root);\n while(!q.empty())\n
xlingcc
NORMAL
2015-07-18T12:02:33+00:00
2015-07-18T12:02:33+00:00
951
false
vector<int> preorderTraversal(TreeNode* root) {\n vector<int> result;\n stack<TreeNode*> q;\n q.push(root);\n while(!q.empty())\n {\n auto p = q.top();\n q.pop();\n if(p == NULL)\n {\n continue;\n }\n ...
6
0
['C++']
0
binary-tree-preorder-traversal
Java solution -- preorder Morris traversal
java-solution-preorder-morris-traversal-v8yga
![preorder Morris traversal][1]\n\n public List preorderTraversal(TreeNode node) {\n List list = new ArrayList();\n \n while(node != nul
peng4
NORMAL
2015-09-29T18:50:52+00:00
2015-09-29T18:50:52+00:00
1,844
false
![preorder Morris traversal][1]\n\n public List<Integer> preorderTraversal(TreeNode node) {\n List<Integer> list = new ArrayList();\n \n while(node != null) {\n if(node.left == null) {\n list.add(node.val);\n node = node.right;\n }\n ...
6
0
[]
3
binary-tree-preorder-traversal
Accepted iterative simplest solution in Java using stack.
accepted-iterative-simplest-solution-in-uzecq
public class Solution {\n public List<Integer> preorderTraversal(TreeNode root) {\n List<Integer> list = new LinkedList<Integer>();\n
xpao24
NORMAL
2016-01-20T11:24:06+00:00
2016-01-20T11:24:06+00:00
973
false
public class Solution {\n public List<Integer> preorderTraversal(TreeNode root) {\n List<Integer> list = new LinkedList<Integer>();\n Stack<TreeNode> stack = new Stack<TreeNode>();\n if(root!=null) stack.push(root);\n while(!stack.isEmpty()){\n TreeN...
6
0
['Java']
1
binary-tree-preorder-traversal
Python one line
python-one-line-by-abyellow7511-sjeg
\nclass Solution(object):\n def preorderTraversal(self, root):\n """\n :type root: TreeNode\n :rtype: List[int]\n """\n\n
abyellow7511
NORMAL
2017-02-20T13:03:11.647000+00:00
2017-02-20T13:03:11.647000+00:00
498
false
```\nclass Solution(object):\n def preorderTraversal(self, root):\n """\n :type root: TreeNode\n :rtype: List[int]\n """\n\n return [root.val]+ self.preorderTraversal(root.left) + self.preorderTraversal(root.right) if root else []\n```
6
0
[]
1
binary-tree-preorder-traversal
Conquering Preorder Traversal: The Ultimate Java and Python Guide! ⚔️
conquering-preorder-traversal-the-ultima-ikra
🌲 "The Tree Kingdom and the Legend of Preorder Traversal!" ⚔️Once upon a time, in the mystical land of Binary Trees, a wandering coder (that’s YOU! 💪) embarked
trivickram_1476
NORMAL
2025-04-01T06:03:01.522959+00:00
2025-04-01T06:09:05.912326+00:00
279
false
# 🌲 "The Tree Kingdom and the Legend of Preorder Traversal!" ⚔️ Once upon a time, in the mystical land of Binary Trees, a wandering coder (that’s YOU! 💪) embarked on a sacred quest… to conquer the kingdom of Preorder Traversal! 👑 The Tree Guardian appeared and whispered the Golden Rule of Preorder: `🗡️ "Root ➝ Le...
5
2
['Stack', 'Tree', 'Depth-First Search', 'Recursion', 'Binary Tree', 'Java', 'Python3']
0
binary-tree-preorder-traversal
Beats 100.00% using c++ without recursive method
beats-10000-using-c-without-recursive-me-1x7k
IntuitionTo perform a preorder traversal (root → left → right), we need to process the root node first, followed by the left and right subtrees. A stack can be
Saumya_Shah_09
NORMAL
2025-01-27T05:05:50.622048+00:00
2025-01-27T05:05:50.622048+00:00
1,038
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> To perform a preorder traversal (root → left → right), we need to process the root node first, followed by the left and right subtrees. A stack can be used to simulate the recursive nature of the traversal, ensuring that nodes are processed...
5
0
['C++']
0
binary-tree-preorder-traversal
Preorder Traversal | C solution
preorder-traversal-c-solution-by-samarth-zq29
IntuitionApproachComplexity Time complexity: O(n) Space complexity: O(n) Code
samarthag01
NORMAL
2025-01-26T09:57:44.585249+00:00
2025-01-26T09:57:44.585249+00:00
474
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> ![Screenshot 2025-01-26 152536.png](https://assets.leetcode.com/users/images/65b6b729-b45a-44d8-988c-8960336c4620_1737885352.6659842.png) # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: O...
5
0
['C']
0
binary-tree-preorder-traversal
C++ ✅ || 5 Lines Code 🎯||Beats 100 %💯 || Recursive Approach🔥
c-5-lines-code-beats-100-recursive-appro-cul2
Preorder Traversal of a Binary Tree 🌳✨ "Every node in DSA is a step toward mastering the tree of knowledge—keep climbing!" 🌟Intuition 🤔Preorder traversal visits
yashm01
NORMAL
2024-12-25T17:05:06.813011+00:00
2024-12-25T17:05:06.813011+00:00
468
false
# Preorder Traversal of a Binary Tree 🌳 # ✨ **"Every node in DSA is a step toward mastering the tree of knowledge—keep climbing!"** 🌟 ![Screenshot 2024-12-25 222635.png](https://assets.leetcode.com/users/images/c1fb4cd2-8a84-4a94-baa1-56febee45bea_1735146293.3581512.png) --- ## Intuition 🤔 Preorder traversal ...
5
0
['Tree', 'Binary Tree', 'C++']
0
binary-tree-preorder-traversal
Easy Approach using C
easy-approach-using-c-by-rishi_2330-2862
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Rishi_2330
NORMAL
2023-11-23T06:47:36.224417+00:00
2023-11-23T06:47:36.224448+00:00
466
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary...
5
0
['C']
0
binary-tree-preorder-traversal
Python 3 | Recursive and Iterative Solutions + Visual Explanation
python-3-recursive-and-iterative-solutio-sqig
Here is the visual explanation for the iterative version.\nI\'m waiting for your comments\uD83D\uDCA1\nUpvote if you like this post!\n\n\n\n\n\n\n# Iterative Ve
malkim
NORMAL
2023-01-09T12:00:58.812510+00:00
2023-01-09T12:06:08.550791+00:00
912
false
Here is the visual explanation for the iterative version.\nI\'m waiting for your comments\uD83D\uDCA1\n**Upvote** if you like this post!\n\n![return (1).png](https://assets.leetcode.com/users/images/c8ae62f3-3b86-4713-b7a1-5f0c37a35f4f_1673265765.4591982.png)\n\n\n\n\n# Iterative Version\n```\n def preorderTraversal...
5
0
['Python3']
0
binary-tree-preorder-traversal
C++ Easy Solution Using Recursion and Moris traversal
c-easy-solution-using-recursion-and-mori-q34j
C++ Easy Solution Using Recursion and Moris traversal\n # Recursive Traversal\n\nclass Solution {\npublic:\n void preorder(TreeNode*&root,vector<int>&pre){\n
Conquistador17
NORMAL
2023-01-09T03:54:58.383714+00:00
2023-01-09T03:54:58.383748+00:00
1,073
false
# **C++ Easy Solution Using Recursion and Moris traversal**\n* # **Recursive Traversal**\n```\nclass Solution {\npublic:\n void preorder(TreeNode*&root,vector<int>&pre){\n if(root){\n pre.push_back(root->val);\n preorder(root->left,pre);\n preorder(root->right,pre);\n }...
5
0
['Recursion', 'C', 'Binary Tree', 'C++']
0
binary-tree-preorder-traversal
✅C++ || Three Solutions || Recursive , Iterative , Morris Traversal
c-three-solutions-recursive-iterative-mo-e8pz
Approach 1\nRecursive Traversal \n# Complexity\n- Time complexity : O(n)\n- Space complexity: O(n) (Function Call Stack)\n# Code\n\nclass Solution {\n void t
Brutcoder
NORMAL
2023-01-09T03:16:01.382282+00:00
2023-01-09T03:16:01.382315+00:00
583
false
# Approach 1\nRecursive Traversal \n# Complexity\n- Time complexity : $$O(n)$$\n- Space complexity: $$O(n)$$ (Function Call Stack)\n# Code\n```\nclass Solution {\n void traverse(TreeNode* root,vector<int>& ans)\n {\n if(root==NULL)return;\n \n ans.push_back(root->val);\n traverse(root-...
5
0
['Stack', 'Tree', 'Depth-First Search', 'Binary Tree', 'C++']
0
binary-tree-preorder-traversal
[Rust] Iterative & recursive | 0ms
rust-iterative-recursive-0ms-by-tmtappr-zxph
Approach\nThe recursive version descends the tree through recursive calls with their state pushed on the execution stack. The traversal prefers the left branch
tmtappr
NORMAL
2023-01-09T01:20:22.272329+00:00
2023-01-09T14:43:48.441074+00:00
643
false
# Approach\nThe recursive version descends the tree through recursive calls with their state pushed on the execution stack. The traversal prefers the left branch over the right, and the output is built before descending either branch.\n\nThe iterative version is very similar. It also uses a stack to preserve the state ...
5
0
['Binary Tree', 'Rust']
1
binary-tree-preorder-traversal
Java | O(n) | 0 MS
java-on-0-ms-by-mdxabu-oyf5
Intuition\n step 1---> add the root element in list\n step 2--->add the left element in list\n step 3--->add the right element in list\n\n# Approach\n
mdxabu
NORMAL
2023-01-09T00:48:56.481222+00:00
2023-01-09T03:19:28.702229+00:00
908
false
# Intuition\n step 1---> add the root element in list\n step 2--->add the left element in list\n step 3--->add the right element in list\n\n# Approach\n Using Recursion\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n\n# Code\n```\n/**\n * Definition for a...
5
0
['Java']
2
binary-tree-preorder-traversal
🗓️ Daily LeetCoding Challenge January, Day 9
daily-leetcoding-challenge-january-day-9-zioz
This problem is the Daily LeetCoding Challenge for January, Day 9. Feel free to share anything related to this problem here! You can ask questions, discuss what
leetcode
OFFICIAL
2023-01-09T00:00:15.282759+00:00
2023-01-09T00:00:15.282826+00:00
7,094
false
This problem is the Daily LeetCoding Challenge for January, Day 9. Feel free to share anything related to this problem here! You can ask questions, discuss what you've learned from this problem, or show off how many days of streak you've made! --- If you'd like to share a detailed solution to the problem, please cr...
5
0
[]
45
binary-tree-preorder-traversal
Beated 94.21% 😎😎 || Javascript || Simple Small Code 😇
beated-9421-javascript-simple-small-code-ulfw
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
sikkasakshi2
NORMAL
2022-12-01T18:54:24.089779+00:00
2022-12-01T18:54:24.089826+00:00
930
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
5
0
['Recursion', 'JavaScript']
4
binary-tree-preorder-traversal
Preorder
preorder-by-louis-chery-xrbv
\nclass Solution {\npublic:\n vector<int> a;\n void preorder(TreeNode* root)\n {\n if(root)\n {\n a.push_back(root->val);\n
louis-chery
NORMAL
2022-11-01T03:25:20.027431+00:00
2022-11-01T03:25:20.027477+00:00
712
false
```\nclass Solution {\npublic:\n vector<int> a;\n void preorder(TreeNode* root)\n {\n if(root)\n {\n a.push_back(root->val);\n preorder(root->left);\n preorder(root->right);\n }\n }\n vector<int> preorderTraversal(TreeNode* root) {\n preorder(r...
5
0
[]
0
construct-the-minimum-bitwise-array-ii
Efficient bitwise O(NlogK)
efficient-bitwise-onlogk-by-david1121-0vb5
Intuition\nI solved this problem by noticing a pattern in the given examples:\n\n\nInput | Answer | Input | Answer\n-------------------------------\n2
david1121
NORMAL
2024-10-12T16:00:54.065088+00:00
2024-10-17T02:49:28.201261+00:00
2,882
false
# Intuition\nI solved this problem by noticing a pattern in the given examples:\n\n```\nInput | Answer | Input | Answer\n-------------------------------\n2 -1 10\n3 1 11 1\n5 4 101 100\n7 3 111 11\n11 9 1011 1001\n13 12...
55
7
['Bit Manipulation', 'C++', 'Java', 'Python3', 'JavaScript']
5
construct-the-minimum-bitwise-array-ii
[Java/C++/Python] Low Bit, O(n)
javacpython-low-bit-on-by-lee215-6dla
Intuition\nans[i] OR (ans[i] + 1) == nums[i]\nans[i] and ans[i] + 1 are one odd and one even.\nnums[i] is an odd.\n\n\n# Explanation\n..0111 || (..0111 + 1) =
lee215
NORMAL
2024-10-12T17:30:12.657926+00:00
2024-10-18T02:46:49.635106+00:00
830
false
# **Intuition**\n`ans[i] OR (ans[i] + 1) == nums[i]`\n`ans[i]` and `ans[i] + 1` are one odd and one even.\n`nums[i]` is an odd.\n<br>\n\n# **Explanation**\n`..0111 || (..0111 + 1) = ..1111`\nThe effect is that change the rightmost 0 to 1.\n\nFor each `a` in array `A`,\nfind the rightmost 0,\nand set the next bit from ...
21
5
['C', 'Python', 'Java']
3
construct-the-minimum-bitwise-array-ii
Explained - Bit wise manipulation || Very simple and easy understand
explained-bit-wise-manipulation-very-sim-68ip
\n# Intuition \n- By analysing few cases I observed following:\n - as all are prime except 2, all numbers are odd ( so the last bit is set)\n - as all numbers
kreakEmp
NORMAL
2024-10-12T16:02:07.162480+00:00
2024-10-12T16:02:07.162505+00:00
1,850
false
\n# Intuition \n- By analysing few cases I observed following:\n - as all are prime except 2, all numbers are odd ( so the last bit is set)\n - as all numbers are odd => trivial ans is n-1. Where n-1 one has last bit set to zero and (n-1) | n will be always eqaul to n\n - Now we need to optimise further to find a s...
17
6
['C++']
4
construct-the-minimum-bitwise-array-ii
Python3 || 11 lines, just odd numbers || T/S: 98% / 64%
python3-11-lines-just-odd-numbers-ts-98-07gpf
Here\'s the intuition:\nThe prime number condition is a red herring. The real condition is the elements of nums are odd integers (excepting of course, 2). Odd n
Spaulding_
NORMAL
2024-10-12T17:42:28.808194+00:00
2024-10-16T21:22:03.877971+00:00
232
false
Here\'s the intuition:\nThe *prime number* condition is a *red herring*. The real condition is the elements of `nums` are odd integers (excepting of course, 2). Odd numbers are either 4 mod 1 or 4 mod 3. The *4 mod 1* case becomes trivial after analyzing some examples. The *4 mod 3* case requires a bit more investigati...
12
2
['C++', 'Java', 'Python3']
0
construct-the-minimum-bitwise-array-ii
Bit Before Leftmost Zero
bit-before-leftmost-zero-by-votrubac-offw
There is no answer for an even number. \n\nFor an odd number n, there is always an answer: n - 1 (since (n - 1) | n == n).\n\nHowever, there could be other answ
votrubac
NORMAL
2024-10-13T02:01:13.434889+00:00
2024-10-13T02:11:48.811544+00:00
217
false
There is no answer for an even number. \n\nFor an odd number `n`, there is always an answer: `n - 1` (since `(n - 1) | n == n`).\n\nHowever, there could be other answers, such as `n - 2`, `n - 4`, `n - 8` and so on.\n\nTo find the minimum answer, let\'s look at example `111` (`0b1101111`):\n- `0b1101111 - 0b0001`; `0b1...
7
2
[]
1
construct-the-minimum-bitwise-array-ii
[Java/Python 3] Bit Manipulation w/ brief explanation and analysis.
javapython-3-bit-manipulation-w-brief-ex-1que
Intuition:\n1. ans[i] | (ans[i] + 1) must be odd, therefore there is no way to find an ans[i] such that ans[i] | (ans[i] + 1) = 2; Hence, return -1 for 2;\n2. B
rock
NORMAL
2024-10-12T16:03:34.060938+00:00
2024-10-13T17:55:43.590026+00:00
437
false
**Intuition:**\n1. `ans[i] | (ans[i] + 1)` must be odd, therefore there is no way to find an `ans[i]` such that `ans[i] | (ans[i] + 1) = 2`; Hence, return `-1` for `2`;\n2. By observation, for any given binary number `0...1...0...11...1`, only the ending `11....1` part can be minimized to `01...1` such that `01...1 + 1...
7
0
['Bit Manipulation', 'Java', 'Python3']
3
construct-the-minimum-bitwise-array-ii
💡 [EASY] INTUITION & EXPLANATION
easy-intuition-explanation-by-ramitgangw-83pz
\n\n# \uD83D\uDC93**_PLEASE CONSIDER UPVOTING_**\uD83D\uDC93\n\n \n\n\n# \u2B50 Problem Overview\nIn this problem, you are tasked with constructing an array ans
ramitgangwar
NORMAL
2024-10-12T16:08:15.687979+00:00
2024-10-12T16:08:15.688003+00:00
372
false
<div align="center">\n\n# \uD83D\uDC93**_PLEASE CONSIDER UPVOTING_**\uD83D\uDC93\n\n</div>\n\n***\n# \u2B50 Problem Overview\nIn this problem, you are tasked with constructing an array `ans` from an array `nums` of prime integers. For each index `i`, the relationship that must hold is `ans[i] | (ans[i] + 1) = nums[i]`....
6
0
['Bit Manipulation', 'Bitmask', 'Java']
1
construct-the-minimum-bitwise-array-ii
[Python3] Brute-Force -> Greedy - Detailed Solution
python3-brute-force-greedy-detailed-solu-asxc
Intuition\n Describe your first thoughts on how to solve this problem. \n- Using pen and draw some examples and recognized the pattern\n\n\nInput | Answer | Inp
dolong2110
NORMAL
2024-10-12T16:53:16.082646+00:00
2024-10-12T16:53:16.082673+00:00
143
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Using pen and draw some examples and recognized the pattern\n\n```\nInput | Answer | Input | Answer\n-------------------------------\n2 -1 10\n3 1 11 1\n5 4 101 100\n7 3...
5
1
['Greedy', 'Bit Manipulation', 'Python3']
1
construct-the-minimum-bitwise-array-ii
Simple power of 2 Solution Java(O(n*32))
simple-power-of-2-solution-javaon32-by-r-wvo6
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n# Complexity\n- Time co
rajnarayansharma110
NORMAL
2024-10-12T17:56:40.477796+00:00
2024-10-12T17:56:40.477818+00:00
13
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n# Complexity\n- Time complexity:O(n*32)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$...
4
0
['Java']
0
construct-the-minimum-bitwise-array-ii
Easy Solution In Java ✅ | 9 Lines | Beats 100% ✅✅✅ In Both Space And Time Complexity!!!!
easy-solution-in-java-9-lines-beats-100-l8d1o
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires generating an array based on bitwise operations. For each element
somgester13
NORMAL
2024-10-14T17:04:57.330597+00:00
2024-10-14T17:04:57.330622+00:00
17
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires generating an array based on bitwise operations. For each element in the input list, the task is to find the smallest number j such that modifying its bits in a specific way results in the original value. To do this, ...
3
0
['Java']
0
construct-the-minimum-bitwise-array-ii
Easy Solution | Explained with Example walk through | Simple logic
easy-solution-explained-with-example-wal-4edp
Approach\n- Loop through the input list of numbers:\nFor each number in the list nums, we call the function minval(num) to find the smallest possible i for that
Guna01
NORMAL
2024-10-12T16:28:27.316238+00:00
2024-10-12T17:28:52.638551+00:00
82
false
# Approach\n- Loop through the input list of numbers:\nFor each number in the list nums, we call the function minval(num) to find the smallest possible i for that number.\n\n- In the minval function:\nWe iterate through each bit of num from the most significant bit (MSB) to the least significant bit (LSB).\nFor each bi...
3
0
['Bit Manipulation', 'Bitmask', 'Java']
0
construct-the-minimum-bitwise-array-ii
Flip the First 1 Before the Leftmost 0
flip-the-first-1-before-the-leftmost-0-b-ddpz
IntuitionTo minimize the array element bitwise, flip the leftmost '1' bit before the first '0' in its binary representation. Special cases include numbers ≤2, w
Saurav_kumar_10
NORMAL
2024-12-29T18:33:58.395713+00:00
2024-12-29T18:33:58.395713+00:00
44
false
# Intuition To minimize the array element bitwise, flip the leftmost '1' bit before the first '0' in its binary representation. Special cases include numbers ≤2, which cannot be minimized further. # Approach 1. **Handle Edge Case**: If `nums[i] <= 2`, push `-1` as it's already minimal. 2. **Iterate and Identify*...
2
0
['Array', 'Bit Manipulation', 'C++']
0
construct-the-minimum-bitwise-array-ii
C++ one line solution | Explained arithmetically
c-one-line-solution-explained-arithmetic-2oi0
Deduction\nIt is impossible to find an answer for even numbers since the bitwise or of two consecutive number is always odd \ni.e.:\n \u274C $6 (0b110)$\n>
alecapiani
NORMAL
2024-11-09T13:06:24.136833+00:00
2024-11-10T16:51:25.512607+00:00
62
false
# Deduction\nIt is impossible to find an answer for even numbers since the bitwise or of two consecutive number is always odd \ni.e.:\n \u274C $6 (0b110)$\n> $4 |_{or} 5 = 5$\n> $5 |_{or} 6 = 7$\n> $6 |_{or} 7 = 7$\n \n \u274C $2 (0b10)$ \n> $0 |_{or} 1 = 1$\n> $1 |_{or} 2 = 3$\n\nBut since all primes ...
2
0
['Bit Manipulation', 'C++']
0
construct-the-minimum-bitwise-array-ii
Concise code | Beats 100%
concise-code-beats-100-by-lokeshwar777-7liy
Code\npython3 []\nclass Solution:\n def minBitwiseArray(self, nums: List[int]) -> List[int]:\n """\n a | a+1 = y = num\n y - a = (some p
lokeshwar777
NORMAL
2024-10-14T05:11:06.805868+00:00
2024-10-14T05:14:38.210840+00:00
35
false
# Code\n```python3 []\nclass Solution:\n def minBitwiseArray(self, nums: List[int]) -> List[int]:\n """\n a | a+1 = y = num\n y - a = (some power of 2) = (1 << i) i from 1 to 30\n a = y - (some power of 2)\n a | a+1 = num\n where a = num - (1<<i) i->1 to 30\n """\n\n ...
2
0
['Math', 'Bit Manipulation', 'Python3']
0
construct-the-minimum-bitwise-array-ii
BEATS 100% | O(n) TIME COMPLEXITY | O(n) SPACE COMPLEXITY | PYTHON | BIT MANIPULATION
beats-100-on-time-complexity-on-space-co-orgu
Complexity\n- Time complexity:\nO(n) because we iterate through all numbers.\n\n- Space complexity:\nO(1)\n\n# Code\npython3 []\nclass Solution:\n def minBit
SergioBear
NORMAL
2024-10-13T13:58:38.895234+00:00
2024-10-13T13:58:38.895269+00:00
38
false
# Complexity\n- Time complexity:\n$$O(n)$$ because we iterate through all numbers.\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```python3 []\nclass Solution:\n def minBitwiseArray(self, nums: List[int]) -> List[int]:\n ans = []\n\n for n in nums:\n if n == 2: # 10 (it\'s exactly this prime i...
2
0
['Bit Manipulation', 'Python3']
0
construct-the-minimum-bitwise-array-ii
cpp bitwise sol
cpp-bitwise-sol-by-madhavgarg2213-mnzc
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach : if num =2 then push -1, otherwise check the last consecutive one from th
madhavgarg2213
NORMAL
2024-10-13T10:05:25.302834+00:00
2024-10-13T10:05:25.302873+00:00
22
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach : if num =2 then push -1, otherwise check the last consecutive one from the right. now if u think ki we make that one 0 and then do OR of the new number with (new no +1) as it will have one on the removed pos for sure. now th...
2
0
['C++']
0
construct-the-minimum-bitwise-array-ii
C++ || Commented ||
c-commented-by-khalidalam980-ka2x
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
khalidalam980
NORMAL
2024-10-12T21:22:04.838546+00:00
2024-10-12T21:22:04.838576+00:00
48
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $O(32*n)$\n\n- Space complexity: $O(n)$ for storing result\n\n# Code\n```cpp []\nclass Solution {\npublic:\n vector<int> minBitwi...
2
0
['Math', 'Bit Manipulation', 'C++']
0
construct-the-minimum-bitwise-array-ii
Easy Bit Manipulation , not the best code. || PYTHON 3 ||
easy-bit-manipulation-not-the-best-code-cke21
Intuition\nI noticed a pattern. Basically, the only even prime number is 2, and it doesn\'t have a number x such that x | x+1 = 2. Now for all the other prime n
rahulSriram_25
NORMAL
2024-10-12T16:46:10.217542+00:00
2024-10-12T16:46:10.217573+00:00
16
false
# Intuition\nI noticed a pattern. Basically, the only even prime number is 2, and it doesn\'t have a number `x` such that `x | x+1 = 2`. Now for all the other prime numbers, we need to understand the pattern in the bit representation.\n\nTake \n`3` => `11` and the answer was `1` => `1`\n`5` => `101` and the answer...
2
0
['Python3']
0
construct-the-minimum-bitwise-array-ii
Java Clean Simple Solution | using BitManipulation
java-clean-simple-solution-using-bitmani-9a5x
Complexity\n- Time complexity:O(n*32)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(n)\n Add your space complexity here, e.g. O(n) \n\n# C
Shree_Govind_Jee
NORMAL
2024-10-12T16:02:05.760198+00:00
2024-10-12T16:02:05.760233+00:00
194
false
# Complexity\n- Time complexity:$$O(n*32)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n public int[] minBitwiseArray(List<Integer> nums) {\n int n = nums.size();\n ...
2
0
['Math', 'Bit Manipulation', 'Bitmask', 'Java']
1
construct-the-minimum-bitwise-array-ii
Bit manipulation || beats 100% || very easy, simple analysis || c++
bit-manipulation-beats-100-very-easy-sim-anmp
IntuitionOur intution is based on observing a pattern. If the number is even then no solution is possible. We can prove this by contradiction, suppose we get a
sanmaika
NORMAL
2025-03-15T19:31:59.646857+00:00
2025-03-15T19:31:59.646857+00:00
13
false
![image.png](https://assets.leetcode.com/users/images/7effbc23-c386-41e0-b591-7780d728e61b_1742066966.7074153.png) # Intuition <!-- Describe your first thoughts on how to solve this problem. --> Our intution is based on observing a pattern. If the number is even then no solution is possible. We can prove this by contr...
1
0
['C++']
0
construct-the-minimum-bitwise-array-ii
BEATS 100% || O(N) EASY SOLUTION :-
beats-100-on-easy-solution-by-rxhabh-6zoz
IntuitionApproachCheck for even numbers:The condition ans[i] OR (ans[i] + 1) = nums[i] is impossible to satisfy for even numbers because the result of ans[i] OR
Rxhabh_
NORMAL
2024-12-24T02:54:21.640731+00:00
2024-12-24T02:54:21.640731+00:00
27
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach Check for even numbers: The condition ans[i] OR (ans[i] + 1) = nums[i] is impossible to satisfy for even numbers because the result of ans[i] OR (ans[i] + 1) is always an odd number, while nums[i] is even. Therefore, for any ev...
1
0
['C++']
0
construct-the-minimum-bitwise-array-ii
Rust bitwise
rust-bitwise-by-maxime002-v4qd
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Maxime002
NORMAL
2024-10-13T00:38:35.212484+00:00
2024-10-13T00:38:35.212517+00:00
12
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Rust']
0
construct-the-minimum-bitwise-array-ii
Very Easy beats 100%
very-easy-beats-100-by-shiv19_03-ntyp
Intuition\nYou need to turn off the rightmost bit from the left side consecutive ones.\n\n# Approach\nFor a number 111000011111 the solution is always1110000011
Shiv19_03
NORMAL
2024-10-12T18:03:04.611487+00:00
2024-10-12T23:57:19.416503+00:00
16
false
# Intuition\nYou need to turn off the rightmost bit from the left side consecutive ones.\n\n# Approach\nFor a number `111000011111` the solution is always`111000001111`.\n\n# Complexity\n- Time complexity:\n$$O(n)$$ --> size of array || Don\'t worry about the solve function the bit length is at max 32.\n\n- Space compl...
1
0
['Bit Manipulation', 'Python3']
0
construct-the-minimum-bitwise-array-ii
One Liner Python3.
one-liner-python3-by-sandeep_p-poad
Approach\nfind leftmost 1 in rightmost sequence of 1s and flip it.\ny=x^(x+1) gives mask including rightmost 0.\nflip second most significant bit of y in x.\n#
sandeep_p
NORMAL
2024-10-12T16:50:20.151419+00:00
2024-10-12T17:09:27.281205+00:00
14
false
# Approach\nfind leftmost 1 in rightmost sequence of 1s and flip it.\n`y=x^(x+1)` gives mask including rightmost 0.\nflip second most significant bit of y in x.\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n# Code\n```python3 []\nclass Solution:\n def minBitwiseArray(self, nums: List[int]) ->...
1
0
['Python3']
0
construct-the-minimum-bitwise-array-ii
Easy Solution | Whithout shift operator | Simple logic
easy-solution-whithout-shift-operator-si-lm03
Approach\nLoop through the input list of numbers:\nFor each number in the list nums, we find the smallest possible i for that number.\n\nWe iterate through each
kbhanu5125
NORMAL
2024-10-12T16:49:46.017886+00:00
2024-10-12T16:49:46.017911+00:00
87
false
# Approach\nLoop through the input list of numbers:\nFor each number in the list nums, we find the smallest possible i for that number.\n\nWe iterate through each bit of num from the most significant bit (MSB) to the least significant bit (LSB).\nFor each bit, we flip that bit (change 1 to 0 or 0 to 1) and check if the...
1
0
['Array', 'String', 'Bit Manipulation', 'Bitmask', 'Java']
1
construct-the-minimum-bitwise-array-ii
C++, bit mask, and I tried to explain
c-bit-mask-and-i-tried-to-explain-by-jai-dxbu
Basics -\n1. All prime numbers are odd except 2.\n2. 2 does not have any solution. Push back -1 for 2.\n3. All odd numbers will always have atleast one solution
jain-priyanshu
NORMAL
2024-10-12T16:33:50.395711+00:00
2024-10-12T16:33:50.395732+00:00
227
false
# Basics -\n1. All prime numbers are odd except 2.\n2. 2 does not have any solution. Push back -1 for 2.\n3. All odd numbers will always have atleast one solution: (x BITWISEOR (x-1)) = x. This works because all odd numbers have lowest bit 1 and the number just before that will be the same number but wit lowest bit as ...
1
0
['Bit Manipulation', 'Bitmask', 'C++']
0