title
stringlengths
1
100
titleSlug
stringlengths
3
77
Java
int64
0
1
Python3
int64
1
1
content
stringlengths
28
44.4k
voteCount
int64
0
3.67k
question_content
stringlengths
65
5k
question_hints
stringclasses
970 values
【Video】Ex-Amazon explains a solution with Python, JavaScript, Java and C++
partition-list
1
1
# Intuition\nCreate a small list and a big list.\n\n---\n\n# Solution Video\n## *** Please upvote for this article. *** \n# Subscribe to my channel from here. I have 245 videos as of August 15th\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\nhttps://youtu.be/rR8weWU-WQM\n\n---\n\n# Appro...
22
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`. You should **preserve** the original relative order of the nodes in each of the two partitions. **Example 1:** **Input:** head = \[1,4,3,2,5,2\], x = 3 **Output:**...
null
Concise Recursion in Python Less than 10 Lines, Faster than 90%
partition-list
0
1
# Complexity\n- Time complexity: $$O(N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:\n def traverse(v, lo=-inf, hi=inf, u=None):\n if not v:\n return u\n ...
1
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`. You should **preserve** the original relative order of the nodes in each of the two partitions. **Example 1:** **Input:** head = \[1,4,3,2,5,2\], x = 3 **Output:**...
null
Commented Code || Line by line explained || C++ || java || Python
partition-list
1
1
\uD83C\uDDEE\uD83C\uDDF3 Happy independence Day \uD83C\uDDEE\uD83C\uDDF3\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThis approach uses two separate lists to partition the nodes.\n\nFor detailed explanation you can refer to my youtube channel (hindi Language)\nhttps://youtu.be/W...
34
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`. You should **preserve** the original relative order of the nodes in each of the two partitions. **Example 1:** **Input:** head = \[1,4,3,2,5,2\], x = 3 **Output:**...
null
Python | Java | Linear Traversal using head and tail nodes
partition-list
1
1
# Intuition\n\n<!-- Describe your first thoughts on how to solve this problem. -->\nLinear Traversal.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nMaintain head and tail node.\n\n# Complexity\n- Time complexity: O(N) where N is the length of the Linked List\n<!-- Add your time complexity here...
1
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`. You should **preserve** the original relative order of the nodes in each of the two partitions. **Example 1:** **Input:** head = \[1,4,3,2,5,2\], x = 3 **Output:**...
null
🦀🐍 Rust + Python 🔗 List Fast Solution
partition-list
0
1
# Intuition \uD83E\uDD14\nImagine you\'re a \uD83E\uDD80 sorting pearls and stones from the ocean floor. The pearls (values less than `x`) are precious and you want to keep them close. The stones (values greater than or equal to `x`), while not as valuable, still need to be stored neatly. Now, imagine each pearl and st...
10
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`. You should **preserve** the original relative order of the nodes in each of the two partitions. **Example 1:** **Input:** head = \[1,4,3,2,5,2\], x = 3 **Output:**...
null
Covert To list first easy.py
partition-list
0
1
# 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
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`. You should **preserve** the original relative order of the nodes in each of the two partitions. **Example 1:** **Input:** head = \[1,4,3,2,5,2\], x = 3 **Output:**...
null
Naive Approach (dummy, Two passes)
partition-list
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nMake two passes and add create nodes into dummy\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> O(n)\n \n- Space complexity:\n...
1
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`. You should **preserve** the original relative order of the nodes in each of the two partitions. **Example 1:** **Input:** head = \[1,4,3,2,5,2\], x = 3 **Output:**...
null
Easy and well define solution//py3.
scramble-string
0
1
# Behind the logic:\nThis problem can be solve by using dynamic programing.We can define a 3-dimensional array dp[l][i][j], where l denotes the length of the substring, i denotes the starting index of the substring in the first string s1, and j denotes the starting index of the substring in the second string s2.\n\nThe...
1
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
Python shortest 2-liner. DP. Functional programming.
scramble-string
0
1
# Approach\nTL;DR, Same as [Editorial solution](https://leetcode.com/problems/scramble-string/editorial/) but written in a functional way.\n\n# Complexity\n- Time complexity: $$O(n^4)$$\n\n- Space complexity: $$O(n^3)$$\n\nwhere, `n is length of s1 or s2`.\n\n# Code\n2-liner (Not readable):\n```python\nclass Solution:\...
2
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
✔💯 DAY 364 | 100% | [JAVA/C++/PYTHON] | EXPLAINED | INTUTION | DRY RUN | PROOF 💫
scramble-string
1
1
\n\n# Happy Sri Ram Navami to all !! \uD83D\uDEA9\uD83D\uDEA9\uD83D\uDEA9\n![image.png](https://assets.leetcode.com/users/images/a2267944-10b8-41f2-b624-81a67ccea163_1680148646.205976.png)\n# NOTE:- if you found anyone\'s post helpful please upvote that post because some persons are downvoting unneccesarily, and you ar...
100
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
Image Explanation🏆- [Recursion -> DP + Complexity Analysis] - C++/Java/Python
scramble-string
1
1
# Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`Scramble String` by `Aryan Mittal`\n![Google5.png](https://assets.leetcode.com/users/images/686e8c0a-9b17-46c6-bb66-fccb88c23d3b_1680160513.964775.png)\n\n\n# Approach & Intution\n![image.png](https://assets.leetcode.com/users/images/722ed5f3-cb83-4348-adad...
42
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
Python3 || 35ms || Beats 99.38% (recursion with memoization)
scramble-string
0
1
The solution uses recursion with memoization to check all possible partitions of the two strings and determine if they are scrambled versions of each other. The memoization is achieved using a dictionary m that stores the result of previous computations for the same inputs.\n\nThe func function takes in two strings s1 ...
73
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
✅✅Python🔥Java 🔥C++🔥Simple Solution🔥Easy to Understand🔥
scramble-string
1
1
# Please UPVOTE \uD83D\uDC4D\n\n**!! BIG ANNOUNCEMENT !!**\nI am currently Giving away my premium content well-structured assignments and study materials to clear interviews at top companies related to computer science and data science to my current Subscribers this week. I planned to give for next 10,000 Subscribers a...
30
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
[Python 3] DFS -> DP Top-down Memoization - Steps by Steps easy to understand
scramble-string
0
1
# 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
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
Scramble String with step by step explanation
scramble-string
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nHere\'s one approach to solve the problem using Dynamic Programming in Python3:\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexit...
3
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
Python Elegant & Short | Top-Down DP | Memoization
scramble-string
0
1
# Complexity\n- Time complexity: $$O(n^{2})$$\n- Space complexity: $$O(n^{2})$$\n\n# Code\n```\nclass Solution:\n def isScramble(self, first: str, second: str) -> bool:\n @cache\n def dp(a: str, b: str) -> bool:\n if a == b:\n return True\n\n if Counter(a) != Counte...
13
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
Sliding Window - Faster than 96%
scramble-string
0
1
# Solution Using Sliding Window\n\nConsider the example:\n\n```\ns1 = \'great\'\ns2 = \'rgeat\'\n```\n\nWe conside a sliding window:\n\n```\ng reat\ngr eat\ngre at\ngrea t\n```\n\nObserve that if any such decomposition is valid, say `gr eat`, then the first component `gr` must have the same letter frequencies as either...
1
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
🟢 Mastering the Art of String Scrambling: An In-Depth Analysis || Mr. Robot
scramble-string
1
1
# Unraveling the Scrambled String Problem with Dynamic Programming\n\n## Problem Statement\n\nThe Scramble String problem presents us with two strings, `s1` and `s2`. We need to determine if `s2` is a scrambled string of `s1`. A scrambled string means that we can split `s1` into two non-empty substrings and swap them t...
1
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
Day 89 || DP || Easiest Beginner Friendly Sol
scramble-string
1
1
**NOTE - PLEASE READ INTUITION AND APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Intuition of this Problem :\n*The problem of determining whether two strings are scrambled versions of each other is a challenging one. One way to approach this pro...
7
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
Recursive python solution beats 75%
scramble-string
0
1
# 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
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
python3 Solution
scramble-string
0
1
\n```\nclass Solution:\n def isScramble(self, s1: str, s2: str) -> bool:\n if len(s1)!=len(s2):\n return False\n m=dict()\n def f(a,b):\n if (a,b) in m:\n return m[(a,b)]\n if a==b:\n m[a,b]=True\n return True\n ...
7
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
[Java/C++/Python/JavaScript/Kotlin/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022
scramble-string
1
1
***Hello it would be my pleasure to introduce myself Darian.***\n\n***Java***\n```\npublic class Solution {\n\tpublic boolean isScramble(String s1, String s2) {\n\t\tif (s1.length() != s2.length()) return false;\n\t\tint len = s1.length();\n\t\t/**\n\t\t * Let F(i, j, k) = whether the substring S1[i..i + k - 1] is a sc...
5
We can scramble a string s to get a string t using the following algorithm: 1. If the length of the string is 1, stop. 2. If the length of the string is > 1, do the following: * Split the string into two non-empty substrings at a random index, i.e., if the string is `s`, divide it to `x` and `y` where `s = x +...
null
#LehanaCodes - Two-Pointer & Three-Pointer Methods || Debug Print Statements | Comment Explanations
merge-sorted-array
0
1
# Features\n\n- All #LehanaCodes are with proper explanation of the intuition.\n- In addition to text explanation, the code is also fully commented to assist coders in understanding each statement. \n- Set `DEBUG` to `True` and you can then visualize the logic as the code executes. \n- Any method (Two-Pointer and Three...
4
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. **Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. The final sorted array should not be re...
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a ti...
1st
merge-sorted-array
0
1
# 1\n\n# Code\n```\nclass Solution:\n def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:\n cnt1 = 0\n cnt2 = 0\n cnt = 0\n \n while cnt1 < m and cnt2 < n:\n \n if nums1[cnt] > nums2[cnt2]:\n for i in range(m + n - 1, cn...
1
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. **Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. The final sorted array should not be re...
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a ti...
Best Python Solution, Faster Than 99%, One Loop, No Splicing, No Special Case Loop
merge-sorted-array
0
1
I was super confused why many solutions had a final check for a special case. This is in-place, and doesn\'t have a weird final loop to deal with any special cases.\n```\ndef merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:\n\ta, b, write_index = m-1, n-1, m + n - 1\n\n\twhile b >= 0:\n\t\tif a ...
488
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. **Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. The final sorted array should not be re...
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a ti...
3 Lines of Code ---> Python3
merge-sorted-array
0
1
# 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)$$ --...
104
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. **Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. The final sorted array should not be re...
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a ti...
Easy 0 ms 100% (Fully Explained)(Java, C++, Python, JS, C, Python3)
merge-sorted-array
1
1
# **Java Solution (Two Pointers Approach):**\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Merge Sorted Array.\n```\nclass Solution {\n public void merge(int[] nums1, int m, int[] nums2, int n) {\n // Initialize i and j to store indices of the last element of 1st and 2nd array respectivel...
138
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. **Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. The final sorted array should not be re...
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a ti...
First test
merge-sorted-array
0
1
# 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)$$ --...
0
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. **Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. The final sorted array should not be re...
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a ti...
e
merge-sorted-array
0
1
# Complexity\n- Time complexity:\n$$O((m+n)log(m+n))$$\n> Due to sort func\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution:\n def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:\n for i in range(n):\n nums1[m+i] = nums2[i]\n nums1.sort()\n retu...
0
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. **Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. The final sorted array should not be re...
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a ti...
2 lines of codes, one more O(m+n) approach
merge-sorted-array
0
1
# 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)$$ --...
2
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. **Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. The final sorted array should not be re...
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a ti...
Merge Sort in 1 line, 2 line and 3 line in Python3
merge-sorted-array
0
1
\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere\'s a step-by-step explanation of the code:\n\n1. The method merge takes two lists, nums1 and nums2, which are expected to contain integer values, and two integers, m and n, which represent the lengths of the portions of nums1 and nums2 that sho...
2
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. **Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. The final sorted array should not be re...
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a ti...
89. Gray Code
gray-code
0
1
# Intuition\nThe Gray code is a binary numeral system where two successive values differ in only one bit. To construct a Gray code sequence, you can use a recursive approach where you build the sequence incrementally.\n\n# Approach\n\n1.Handle the base case when n is 0 by returning [0].\n2.For n > 0, you can generate a...
1
An **n-bit gray code sequence** is a sequence of `2n` integers where: * Every integer is in the **inclusive** range `[0, 2n - 1]`, * The first integer is `0`, * An integer appears **no more than once** in the sequence, * The binary representation of every pair of **adjacent** integers differs by **exactly one ...
null
Gray Code with step by step explanation
gray-code
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Create an initial list with 0\n2. For each bit in n, starting from the 0th bit:\n 1. Traverse the current list in reverse and add the current bit to each number by doing bitwise OR with (1 << i)\n 2. This step is repe...
5
An **n-bit gray code sequence** is a sequence of `2n` integers where: * Every integer is in the **inclusive** range `[0, 2n - 1]`, * The first integer is `0`, * An integer appears **no more than once** in the sequence, * The binary representation of every pair of **adjacent** integers differs by **exactly one ...
null
Python3
gray-code
0
1
# 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- O(N+2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(N)\n<!-- Add your space complexity here...
3
An **n-bit gray code sequence** is a sequence of `2n` integers where: * Every integer is in the **inclusive** range `[0, 2n - 1]`, * The first integer is `0`, * An integer appears **no more than once** in the sequence, * The binary representation of every pair of **adjacent** integers differs by **exactly one ...
null
[Python3] 1 Line Solution: List Comprehension
gray-code
0
1
Using List Comprehension, we can easily solve this problem statement in 1 line.\n```\n# Gray Code Formula:\n# n <= 16\n# Gray_Code(n) = n XOR (n / 2)\n\nclass Solution:\n def grayCode(self, n: int) -> List[int]:\n result = [i ^ (i // 2) for i in range(pow(2, n))]\n return result\n```
10
An **n-bit gray code sequence** is a sequence of `2n` integers where: * Every integer is in the **inclusive** range `[0, 2n - 1]`, * The first integer is `0`, * An integer appears **no more than once** in the sequence, * The binary representation of every pair of **adjacent** integers differs by **exactly one ...
null
Python/JS/C++/Go O(2^n) by toggle bitmask [w/ Example]
gray-code
0
1
Python O(2^n) by toggle bitmask\n\n---\n\nExample with n = 2:\n\n1st iteration\n```\n\u30000 0\n\u2295 0 0\n\u2014\u2014\u2014\u2014\u2014\n\u30000 0 \n```\n \n We get 0\'b 00 = **0**\n\n---\n\n2nd iteration\n```\n\u30000 1\n\u2295 0 0\n\u2014\u2014\u2014\u2014\u2014\n\u30000 1 \n```\n \n We get 0\'b 01 = **1**\n\n...
10
An **n-bit gray code sequence** is a sequence of `2n` integers where: * Every integer is in the **inclusive** range `[0, 2n - 1]`, * The first integer is `0`, * An integer appears **no more than once** in the sequence, * The binary representation of every pair of **adjacent** integers differs by **exactly one ...
null
5 line python solution that beats 99.52% of all solutions on runtime
gray-code
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea here is to use mathematical induction, i.e., given the solution for $$n=k$$, construct the solution for $$n=k+1$$.\nObserve that for $$n=1$$, the solution is $$[0, 1]$$. The solution for $$n=2$$ can be constucted by first prepend...
2
An **n-bit gray code sequence** is a sequence of `2n` integers where: * Every integer is in the **inclusive** range `[0, 2n - 1]`, * The first integer is `0`, * An integer appears **no more than once** in the sequence, * The binary representation of every pair of **adjacent** integers differs by **exactly one ...
null
Python Intuitive Solution 🔥❤️‍🔥
subsets-ii
0
1
# Intuition\nGiven a list of integers, the task is to find all possible subsets. However, there are duplicate integers in the list, so we have to handle them to avoid duplicate subsets.\n\n# Approach\n1. First, we sort the list. This ensures that duplicates are adjacent to each other.\n2. We use a backtracking approach...
3
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
Subsets II with step by step explanation
subsets-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution uses a dfs approach to find all the subsets. The algorithm sorts the input list nums to handle duplicates. It starts from the first index start of the list and adds the element at start to the current path. It ...
2
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
Solution
subsets-ii
1
1
```C++ []\nclass Solution {\npublic:\n void subset(vector<int>& nums, int i, vector<int> &l, vector<vector<int>> &ans){\n ans.push_back(l); \n for (int j = i ; j < nums.size() ; j++){\n if (j>i && nums[j] == nums[j-1]) continue;\n l.push_back(nums[j]);\n subset(nums, j+...
1
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
[Python3] Simple Recursive Solution
subsets-ii
0
1
# Intuition\nThe intuition behind the code is to use a recursive approach to generate subsets. At each step, we have two choices: either include the current element in the subset or exclude it. By making these choices for each element in the array, we can generate all possible subsets.\n\n# Approach\n**Sort the array n...
6
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
Same Approches as Subset I with Brute force and Backtracking
subsets-ii
0
1
# 1. Subset 1 Approach-->TC:[2^N*N]\n```\nclass Solution:\n def subsets(self, nums: List[int]) -> List[List[int]]:\n list1 = [[]]\n n=len(nums)\n for i in range(1,2**n):\n list2=[]\n for j in range(n):\n if (i&1<<j):\n list2.append(nums[j])...
4
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
[Python] Clean backtracking with sets
subsets-ii
0
1
# Intuition\nAny time I am trying to find permutations using items from a set, I first try to determine whether I can use backtracking. It turns out this works fine in this case, since we know the input array size is 10 or less.\n\n# Approach\nIn order to avoid duplicates, we should first sort the array of nums. Then w...
3
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
Solution with explanation
subsets-ii
1
1
\n\n# Approach\n- The function fun is a recursive function that generates subsets by considering each element of the array at a particular index.\n- The base case of the recursion is when the index reaches the size of the array. At this point, the current subset (ds) is sorted and inserted into the set res to eliminate...
2
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
Easy & Clear Solution Python3
subsets-ii
0
1
\n\n# Code\n```\nclass Solution:\n def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:\n nums = sorted(nums)\n res = []\n self.backtracking(res,0,[],nums)\n return res\n def backtracking(self,res,start,subset,nums):\n res.append(list(subset))\n for i in range(st...
5
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
Python || 99.97% Faster || Recursion || Easy
subsets-ii
0
1
```\nclass Solution:\n def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:\n s=set()\n nums.sort() # Sort the input list to group duplicates together\n def solve(ip,op):\n if len(ip)==0:\n s.add(op)\n return \n op1=op\n op2...
1
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
General Backtracking questions solutions in Python for reference :
subsets-ii
0
1
I have taken solutions of @caikehe from frequently asked backtracking questions which I found really helpful and had copied for my reference. I thought this post will be helpful for everybody as in an interview I think these basic solutions can come in handy. Please add any more questions in comments that you think mig...
64
Given an integer array `nums` that may contain duplicates, return _all possible_ _subsets_ _(the power set)_. The solution set **must not** contain duplicate subsets. Return the solution in **any order**. **Example 1:** **Input:** nums = \[1,2,2\] **Output:** \[\[\],\[1\],\[1,2\],\[1,2,2\],\[2\],\[2,2\]\] **Example...
null
O(n) time O(1) space solution explained in detail
decode-ways
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs with all other dp problems I did before I like to use a function to represent relationships between subproblems. Let $$DW(n)$$ be the number of ways to decode the substring with n characters. We have two possible methods to decode at a...
2
A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping: 'A' -> "1 " 'B' -> "2 " ... 'Z' -> "26 " To **decode** an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For examp...
null
Python solution
decode-ways
0
1
# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def numDecodings(self, s: str) -> int:\n sset = {str(i) for i in range(1,27)}\n if s[0] == "...
2
A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping: 'A' -> "1 " 'B' -> "2 " ... 'Z' -> "26 " To **decode** an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For examp...
null
🔥 [LeetCode The Hard Way] 🔥 Explained Line By Line
decode-ways
1
1
Please check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. \nI\'ll explain my solution line by line daily and you can find the full list in my [Discord](https://discord.gg/Nqm4jJcyBf).\nIf you like it, please give a star, watch my [Github R...
45
A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping: 'A' -> "1 " 'B' -> "2 " ... 'Z' -> "26 " To **decode** an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For examp...
null
⌛🟢🛰️🟢Python3💎91. Decode Ways🏁1-DP👓O(n) & O(1)🏆Illustration🎉
decode-ways
0
1
# Illustration\n![image.png](https://assets.leetcode.com/users/images/b94a646a-a50b-4afc-982c-779dba8bb110_1689235309.7620294.png)\n![Untitled.png](https://assets.leetcode.com/users/images/c58468a2-0da8-44d8-bb11-fa68a7c36780_1689235944.212471.png)\n# Intuition\n1-DP, similiar to $$Climbing Stairs$$\n\n# Approach\n1-DP...
1
A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping: 'A' -> "1 " 'B' -> "2 " ... 'Z' -> "26 " To **decode** an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For examp...
null
✅ 92.40% Two Pointers & Stack & Recursion
reverse-linked-list-ii
1
1
# Interview Guide - Reversing a Sublist of a Linked List: A Comprehensive Analysis for Programmers\n\n## Introduction & Problem Statement\n\nWelcome to this detailed guide, where we delve into the fascinating problem of reversing a sublist within a singly-linked list. The challenge is not just to reverse a sublist but ...
103
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_. **Example 1:** **Input:** head = \[1,2,3,4,5\], left = 2, right = 4 **Output:** \[1,4,3,2,5\] **Example 2:** **I...
null
Python Solution
reverse-linked-list-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n# Code\n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseBetween(self, head: Optional[List...
1
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_. **Example 1:** **Input:** head = \[1,2,3,4,5\], left = 2, right = 4 **Output:** \[1,4,3,2,5\] **Example 2:** **I...
null
【Video】Beat 97.21% - Python, JavaScript, Java, C++
reverse-linked-list-ii
1
1
This Python solution beats 97.21%.\n\n![Screen Shot 2023-09-07 at 9.31.46.png](https://assets.leetcode.com/users/images/7dd9d408-b0b0-495a-a052-ec386f5e8fdc_1694046724.5898378.png)\n\n\n---\n\n# Solution Video\n\n### Please subscribe to my channel from here. I have 257 videos as of September 7th, 2023.\n\n### In the vi...
34
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_. **Example 1:** **Input:** head = \[1,2,3,4,5\], left = 2, right = 4 **Output:** \[1,4,3,2,5\] **Example 2:** **I...
null
🔥BEATS 100% |⚡Python⚡ C++⚡Java | ✅Easy Linked List
reverse-linked-list-ii
1
1
\n```Python []\nclass Solution(object):\n def reverseBetween(self, head, left, right):\n if left >= right:\n return head\n \n ohead = newhd = ListNode(0)\n whead = wlast = head\n newhd.next = head\n for i in range(right-left):\n wlast = wlast.next\n ...
2
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_. **Example 1:** **Input:** head = \[1,2,3,4,5\], left = 2, right = 4 **Output:** \[1,4,3,2,5\] **Example 2:** **I...
null
Simple python code using list
reverse-linked-list-ii
0
1
# 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
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_. **Example 1:** **Input:** head = \[1,2,3,4,5\], left = 2, right = 4 **Output:** \[1,4,3,2,5\] **Example 2:** **I...
null
✔Mastering Linked List Reversal ✨|| A Step-by-Step Guide📈🧠|| Easy to understand || #Beginner😉😎
reverse-linked-list-ii
1
1
# PROBLEM UNDERSTANDING:\n- **The code appears** to be a C++ implementation of a function `reverseBetween `within a class `Solution`. \n- This function takes as input a singly-linked list represented by a `ListNode` and two integers, `left` and `right`. \n- It aims to reverse the elements of the linked list from the `l...
20
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_. **Example 1:** **Input:** head = \[1,2,3,4,5\], left = 2, right = 4 **Output:** \[1,4,3,2,5\] **Example 2:** **I...
null
Drawing explanation | easy to understand
reverse-linked-list-ii
1
1
# Intuition and Approach\n\n 1. Find left node indcated by index - l\n 2. Find right node indicated by index - r\n 3. Revers left node till right node\n 4. Link the broken links\n\n Note: Add a dummy head to track left_prev node in case left node is at head\n\n#### Please bear with my diagrams :)\n\nInpu...
5
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_. **Example 1:** **Input:** head = \[1,2,3,4,5\], left = 2, right = 4 **Output:** \[1,4,3,2,5\] **Example 2:** **I...
null
Python short and clean. Single pass. 2 approaches. Functional programming.
reverse-linked-list-ii
0
1
# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\nwhere, `n is number of nodes in LinkedList starting at head`.\n\n# Code\nImperative single-pass:\n```python\nclass Solution:\n def reverseBetween(self, head: ListNode | None, left: int, right: int) -> ListNode | None:\n new_head = Li...
1
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_. **Example 1:** **Input:** head = \[1,2,3,4,5\], left = 2, right = 4 **Output:** \[1,4,3,2,5\] **Example 2:** **I...
null
[Python] Just DFS and backtracking; Explained
restore-ip-addresses
0
1
This is a common DFS and backtracking problem. \n\nWe just need to take care of special requirements:\n(1) the total number of fields is 4;\n(2) no leading \'0\' for a number if it is not \'0\'\n\n```\nclass Solution:\n def restoreIpAddresses(self, s: str) -> List[str]:\n # dfs and backtracking\n self....
2
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
Straight-forward backtrack solution in python3
restore-ip-addresses
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\njust take care with the processing number should be lower than len(s) and the length of current stack should be lower or equal 4.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!...
1
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
Python solution using dfs
restore-ip-addresses
0
1
# Code\n```\nclass Solution:\n\n @cache\n def solve(self , i ,prev , ip) :\n\n if i >= len(self.s) :\n if ip.count(".") == 3 :\n l = list(map(str , ip.split(".")))\n\n lis = [1 if ( x!="" and (x[0]!="0" or x=="0") and int(x)>=0 and int(x)<=255 ) else 0 for x in l ]...
1
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
Daily LeetCode Challenge
restore-ip-addresses
0
1
# Intuition\nWhenever we see a question to find various combinations we can use backtracking. Similarly this problem uses backtracking to find all combinations of valid IP chunks to sum up as valid IP address.\n\n# Approach\nThe must conditions :-\n1. IP address should have only 4chunks\n2. If using more than 1num toge...
1
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
Python 95% runtime and 99% space efficient solution
restore-ip-addresses
0
1
# Approach\nBacktracking\n\n# Code\n```\nclass Solution:\n def __init__(self) -> None:\n self.validIPs = []\n\n def restoreIpAddresses(self, s: str) -> List[str]:\n self.restoreOctets(s)\n return self.validIPs\n\n def restoreOctets(self, s: str, octetNum: int = 1, prevOctets: str = "") -> ...
1
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
🚀Beats 100%(0ms) ||🚀Easy Solution🚀||🔥Fully Explained🔥|| C++ || Python3 || Java
restore-ip-addresses
1
1
# Consider\uD83D\uDC4D\n```\n Please Upvote If You Find It Helpful.\n\n```\n# Intuition\n1. There needs to be 4 parts per IP. There are always three dots/four candidates (2.5.5.2.5.5.1.1.1.3.5 is not valid, neither is 1.1.1)\n2. When selecting a candidate, it can\'t have more than 3 characters (2552....
82
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
Easy Python solution ...Runtime faster than 100% and memory efficient than 99.69%
restore-ip-addresses
0
1
# Intuition\nThe approach intuitively works by breaking down the input string into segments representing parts of an IP address (e.g., four segments for IPv4) and explores all possible combinations of these segments. It uses a recursive function to iterate through the string, considering one, two, or three characters a...
0
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
💡💡 Neatly coded backtracking solution in python3
restore-ip-addresses
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse backtracking to validate the numbers in the IP address. If it is a valid one, add a "." since we\'re validating IPv4 addresses. We should also make sure that thes number don\'t have leading zeroes.\n\n# Approach\n<!-- Describe your ap...
1
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
Backtracking solution to generate all possible valid IP addresses from a given string
restore-ip-addresses
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires us to form all possible valid IP addresses by inserting dots in the given string. A valid IP address has four segments, and each segment should be between 0 and 255 (inclusive) and should not have leading zeros. The i...
1
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
Clean Codes🔥🔥|| Full Explanation✅|| Recursion ✅|| C++|| Java || Python3
restore-ip-addresses
1
1
# Exaplanation to Approach :\n1. The main idea to solve this problem is to use Recursion.\n2. Consider every position of the input string and, there exist two possible cases:\n - Place a dot over the current position.\n - Take this character, i.e don\u2019t place the dot over this position.\n3. At every step of t...
29
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
✔️ 0ms runtime code | C++ | Python | explained
restore-ip-addresses
0
1
# Solution:\n\n``` C++ []\nclass Solution {\npublic:\n bool check(string s){\n int n=s.size();\n //if the size of string is 1 that is always possible so return true\n if(n==1){\n return true;\n }\n //if we have length >3 or string starts with 0 return false\n if(n...
5
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. * For example, `"0.1.2.201 "` and `"192.168.1.1 "` are **valid** IP addresses, but `"0.011.255.245 "`, `"192.168.1.312 "` and `"192.168@1.1 "` are **...
null
【Video】Give me 5 minutes Recursion and Stack(Bonus) solution - how we think about a solution.
binary-tree-inorder-traversal
1
1
# Intuition\nWe should know how to implement inorder, preorder and postorder.\n\n# Approach\n\n---\n\n# Solution Video\n\nhttps://youtu.be/TKMxiXRzDZ0\n\n\u25A0 Timeline\n`0:04` How to implement inorder, preorder and postorder\n`1:16` Coding (Recursive)\n`2:14` Time Complexity and Space Complexity\n`2:37` Explain Stack...
35
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
✅☑[C++/Java/Python/JavaScript] || 3 Approaches || Beats 100% || EXPLAINED🔥
binary-tree-inorder-traversal
1
1
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(Recursive Approach)***\n1. **printVector Function:**\n\n - This function takes a reference to a vector of integers as input and prints its elements in a specific format.\n - It iterates through the vector...
5
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Simple python Solution
binary-tree-inorder-traversal
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nrecursion\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. ...
3
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Recursive sol is trivial, could you do it iteratively? Iterative with explanation| Stack | Recursion
binary-tree-inorder-traversal
0
1
## Video solution \n\nhttps://youtu.be/hgwM2a9Mb_o?si=d8N3TrQzSRxQEkT3\n\n\nPlease subscribe to my channel if it helped you.\nhttps://www.youtube.com/channel/UCsu5CDZmlHcpH7dQozZWtQw?sub_confirmation=1\n\nCurrent subscribers: 213\nTarget subscribers: 250\n\n# Iterative Approach\n\n# Intuition:\n\n- The approach uses an...
2
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Easy Solution
binary-tree-inorder-traversal
0
1
\n\n# Code\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 inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n result =...
2
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Simple solution with Binary Tree Inorder Traversal in Python3
binary-tree-inorder-traversal
0
1
# Approach\nWe have Binary Tree `root` to perform Inorder Traversal.\n\n# Complexity\n- Time complexity: **O(N)**\n\n- Space complexity: **O(N)**\n\n# Code\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.lef...
1
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Solution
binary-tree-inorder-traversal
1
1
```C++ []\nclass Solution {\npublic:\n vector<int> inorderTraversal(TreeNode* root) {\n vector<int> ans;\n if (root == NULL) return ans;\n vector<int> left = inorderTraversal(root->left);\n ans.insert(ans.end(), left.begin(), left.end());\n ans.push_back(root->val);\n vector<int> right = inorderTra...
473
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
🔍 Mastering In-Order Traversal: 3 Approaches, Detailed Explanation, and Insightful Dry Runs 🔥
binary-tree-inorder-traversal
1
1
# \uD83C\uDF32 Exploring Inorder Tree Traversal \uD83C\uDF33\n\n---\n\n## \uD83D\uDCBB Solution\n\n### \uD83D\uDCA1Approach\n\nThe solution defines a class `Solution` with a method `inorderTraversal` for performing inorder tree traversal. The algorithm uses recursion to traverse the tree in an inorder manner, collectin...
5
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Python3 Solution
binary-tree-inorder-traversal
0
1
\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 inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n if root==None:\n ...
4
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
InorderTraversal of Binary Tree
binary-tree-inorder-traversal
0
1
# Code\n```\nclass Solution:\n def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n answer = []\n self.printInorder(root, answer)\n return answer\n \n def printInorder(self, node, answer):\n if node is None:\n return\n\n self.printInorder(node.left,...
1
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
⭐One-Liner In-order Traversal in 🐍|| Basic Idea Implementation⭐
binary-tree-inorder-traversal
0
1
# Code\n```py\nclass Solution:\n def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) if root else []\n```\n\n---\n\n_If you like the solution, promote it._
1
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Python || Basic Inorder || Recursion
binary-tree-inorder-traversal
0
1
# Code\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 inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n\n l = []\n\n...
1
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
📌|| STRIVER || DFS || RECURSION || BEATS(100%) || 0ms || 📌
binary-tree-inorder-traversal
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![image.png](https://assets.leetcode.com/users/images/6307fc35-9d9e-4ae3-816f-e42bc03b702a_1702094835.7734187.png)\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nRecursive Approach:\n\n1)One of the most straig...
2
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
✅ One Line Solution - First Published Here!
binary-tree-inorder-traversal
0
1
# Code #1 - Oneliner On Generator\n```\nclass Solution:\n def inorderTraversal(self, r: Optional[TreeNode]) -> List[int]:\n yield from chain(self.inorderTraversal(r.left), (r.val,), self.inorderTraversal(r.right)) if r else ()\n```\n\n# Code #2 - Classic Oneliner On Lists Concatenation\nTime complexity: $$O(n...
6
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
All DFS traversals (preorder, inorder, postorder) in Python in 1 line
binary-tree-inorder-traversal
0
1
![image](https://assets.leetcode.com/users/andvary/image_1556551007.png)\n\n```\ndef preorder(root):\n return [root.val] + preorder(root.left) + preorder(root.right) if root else []\n```\n\n```\ndef inorder(root):\n return inorder(root.left) + [root.val] + inorder(root.right) if root else []\n```\n\n```\ndef postord...
1,008
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
One-liner beats 96.65% 🔥🥶🔥🥶🔥🥶
binary-tree-inorder-traversal
0
1
![LeetcodeTime.jpg](https://assets.leetcode.com/users/images/438322e9-c621-4925-8725-048507369779_1702122473.9344573.jpeg)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIn order traversal = Left, Root, Right\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $...
0
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
🔥 Easy solution | JAVA | Python 3 🔥|
binary-tree-inorder-traversal
1
1
# Intuition\nBinary Tree Inorder Traversal\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialization:\n\n 1. Initialize an empty stack and a reference variable current to the root node.\n 1. Create an empty list result to store the inorder traversal sequence.\n\n\n1. It...
1
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Recursive Inorder Traversal
binary-tree-inorder-traversal
0
1
# 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
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Python 3 - All Iterative Traversals - InOrder, PreOrder, PostOrder - Similar Solutions
binary-tree-inorder-traversal
0
1
[Python3] Pre, In, Post Iteratively Summarization\nIn preorder, the order should be\n\nroot -> left -> right\n\nBut when we use stack, the order should be reversed:\n\nright -> left -> root\n\nPre\n```\nclass Solution:\n def preorderTraversal(self, root: TreeNode) -> List[int]:\n res, stack = [], [(root, Fals...
351
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
😎Brute Force to Efficient Method🤩 | Java | C++ | Python | JavaScript |
binary-tree-inorder-traversal
1
1
# 1st Method :- Brute force\n\n## Code\n``` Java []\nclass Solution {\n public List<Integer> inorderTraversal(TreeNode root) {\n //store the elements of the binary tree in inorder traversal order.\n List<Integer> result = new ArrayList<>();\n inorderHelper(root, result);\n return result;\...
16
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Recursive and Iterative Approach
binary-tree-inorder-traversal
0
1
# 1. Recursive Approach\n```\nclass Solution:\n def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n ans=[]\n def inorder(root,ans):\n if not root:\n return None\n inorder(root.left,ans)\n ans.append(root.val)\n inorder(root.rig...
37
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
👹 Simple python 1 line code, explination , python, java🐍 ☕
binary-tree-inorder-traversal
1
1
# Intuition\nidea is recursive search for nodes\n# Approach\nin order means: left, mid, right nodes, which is reccursively the same meaning.\nyou go left most node of tree, traverse it, then go back and search root and right values of that subtree, then check again to a level higher than previous layer, and so on, when...
2
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
Morris Inorder Traversal
binary-tree-inorder-traversal
0
1
My Python approach for Morris Inorder Traversal.\nBased on [this](https://www.youtube.com/watch?v=80Zug6D1_r4) video.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution:\n def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n ans = []\n cu...
5
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_. **Example 1:** **Input:** root = \[1,null,2,3\] **Output:** \[1,3,2\] **Example 2:** **Input:** root = \[\] **Output:** \[\] **Example 3:** **Input:** root = \[1\] **Output:** \[1\] **Constraints:** * The number of nodes i...
null
🦀 Unique Binary Search Trees II - A Rusty Approach
unique-binary-search-trees-ii
0
1
Have you ever tried to generate all unique binary search trees (BSTs) with a given number of nodes? It\'s a crab-tastic challenge, but don\'t worry; Rust is here to help you out!\n\n## Intuition\nImagine you have \\( n \\) distinct numbers. You can pick any number as the root of the BST. Once you pick a root, the remai...
4
Given an integer `n`, return _all the structurally unique **BST'**s (binary search trees), which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`. Return the answer in **any order**. **Example 1:** **Input:** n = 3 **Output:** \[\[1,null,2,null,3\],\[1,null,3,2\],\[2,1,3\],\[3,1,null,null,2\],\[3,2,null,1\...
null
✅ Recursion & DP [VIDEO] Catalan Number - Unique BST
unique-binary-search-trees-ii
1
1
Given an integer \\( n \\), the task is to return all the structurally unique BST\'s (binary search trees) that have exactly \\( n \\) nodes of unique values from \\( 1 \\) to \\( n \\).\n\n# Intuition Recursion & Dynamic Programming\nThe problem can be solved by utilizing the properties of a BST, where the left subtre...
78
Given an integer `n`, return _all the structurally unique **BST'**s (binary search trees), which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`. Return the answer in **any order**. **Example 1:** **Input:** n = 3 **Output:** \[\[1,null,2,null,3\],\[1,null,3,2\],\[2,1,3\],\[3,1,null,null,2\],\[3,2,null,1\...
null
Ex-Amazon explains a solution with a video, Python, JavaScript, Java and C++
unique-binary-search-trees-ii
1
1
# Intuition\nThere 3 important things. \n\nOne is we try to create subtrees with some range between start(minimum) and end(maximum) value.\n\nSecond is calculation of the range. left range should be between start and current root - 1 as end value because all values of left side must be smaller than current root. right ...
24
Given an integer `n`, return _all the structurally unique **BST'**s (binary search trees), which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`. Return the answer in **any order**. **Example 1:** **Input:** n = 3 **Output:** \[\[1,null,2,null,3\],\[1,null,3,2\],\[2,1,3\],\[3,1,null,null,2\],\[3,2,null,1\...
null
Using explicit memoization, not using lru_cache, surpassing 90%
unique-binary-search-trees-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. --> using recursion with memoization\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 ...
2
Given an integer `n`, return _all the structurally unique **BST'**s (binary search trees), which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`. Return the answer in **any order**. **Example 1:** **Input:** n = 3 **Output:** \[\[1,null,2,null,3\],\[1,null,3,2\],\[2,1,3\],\[3,1,null,null,2\],\[3,2,null,1\...
null
94.15% Unique Binary Search Trees II with step by step explanation
unique-binary-search-trees-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTo generate all structurally unique BST\'s with n nodes, we can use a recursive approach. The idea is to fix each number i (1 <= i <= n) as the root of the tree and then recursively generate all the left and right subtrees t...
7
Given an integer `n`, return _all the structurally unique **BST'**s (binary search trees), which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`. Return the answer in **any order**. **Example 1:** **Input:** n = 3 **Output:** \[\[1,null,2,null,3\],\[1,null,3,2\],\[2,1,3\],\[3,1,null,null,2\],\[3,2,null,1\...
null
Python3 Solution
unique-binary-search-trees-ii
0
1
\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 generateTrees(self, n: int) -> List[Optional[TreeNode]]:\n @lru_cache\n def ...
4
Given an integer `n`, return _all the structurally unique **BST'**s (binary search trees), which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`. Return the answer in **any order**. **Example 1:** **Input:** n = 3 **Output:** \[\[1,null,2,null,3\],\[1,null,3,2\],\[2,1,3\],\[3,1,null,null,2\],\[3,2,null,1\...
null
Pythonic solution 94.46%
unique-binary-search-trees-ii
0
1
# Approach\nRecursive and Memo\n\n# Code\n```\nclass Solution:\n def generateTrees(self, n: int) -> List[Optional[TreeNode]]:\n \n @cache\n def getTreeList(nodes):\n if not nodes:\n return [None]\n res = []\n for i, val in enumerate(nodes):\n ...
3
Given an integer `n`, return _all the structurally unique **BST'**s (binary search trees), which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`. Return the answer in **any order**. **Example 1:** **Input:** n = 3 **Output:** \[\[1,null,2,null,3\],\[1,null,3,2\],\[2,1,3\],\[3,1,null,null,2\],\[3,2,null,1\...
null