post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3
values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/2280027/Python3-Easy-to-Understand | class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if not preorder or not inorder: return None
root = TreeNode(preorder[0])
pivot = inorder.index(preorder[0])
root.left = self.buildTree(preorder[1: pivot+1], inorder[:... | construct-binary-tree-from-preorder-and-inorder-traversal | ✅Python3 Easy to Understand | thesauravs | 0 | 6 | construct binary tree from preorder and inorder traversal | 105 | 0.609 | Medium | 700 |
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/2263784/Python-Easy-Solution | class Solution(object):
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
if not preorder or not inorder:
return None
root = TreeNode(preorder[0])
mid = inorder.index(preorde... | construct-binary-tree-from-preorder-and-inorder-traversal | Python Easy Solution | Abhi_009 | 0 | 58 | construct binary tree from preorder and inorder traversal | 105 | 0.609 | Medium | 701 |
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/2005900/Python-or-Recursion-or-O(n)-time-O(n)-space | class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
def build(preorder, inorder, l, r):
if l == r:
return
root_val = preorder.pop(0)
root_index = inorder.index(root_val)
root = TreeN... | construct-binary-tree-from-preorder-and-inorder-traversal | Python | Recursion | O(n) time O(n) space | Kiyomi_ | 0 | 45 | construct binary tree from preorder and inorder traversal | 105 | 0.609 | Medium | 702 |
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/1870816/Recursive-or-Step-wise-clear-approach-or-Python3 | class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
#EdgeCase
if len(preorder) == 0 : return
rootData = preorder[0]
root = TreeNode(rootData)
#Finding Index
rootIndex = -1
for... | construct-binary-tree-from-preorder-and-inorder-traversal | Recursive | Step-wise clear approach | Python3 | athrvb | 0 | 44 | construct binary tree from preorder and inorder traversal | 105 | 0.609 | Medium | 703 |
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/1853275/DFS-left-right-boundary | class Solution(object):
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
inmap={}
for i,v in enumerate(inorder):
inmap[v]=i
return self.helper... | construct-binary-tree-from-preorder-and-inorder-traversal | DFS left right boundary | muzhang90 | 0 | 28 | construct binary tree from preorder and inorder traversal | 105 | 0.609 | Medium | 704 |
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/1828578/slow-and-easy-to-understand-python-stack-solution | class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
def helper(preorder, inorder):
if not preorder:
return None
ans = TreeNode(val = preorder.pop(0))
if not preorder:
return ans
le... | construct-binary-tree-from-preorder-and-inorder-traversal | slow and easy to understand python stack solution | 752937603 | 0 | 35 | construct binary tree from preorder and inorder traversal | 105 | 0.609 | Medium | 705 |
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/1738888/Simple-python-code | class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
def buildTree(preorder, inorder):
if inorder:
root = TreeNode(preorder.pop(0))
root_index = inorder.index(root.val)
root.left = buildTree(preorder,i... | construct-binary-tree-from-preorder-and-inorder-traversal | Simple python code | Siddharth_singh | 0 | 136 | construct binary tree from preorder and inorder traversal | 105 | 0.609 | Medium | 706 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/2098606/Python3-O(n)-Time-O(1)-Space-Solution-faster-than-95 | class Solution:
def buildTree(self, inorder, postorder):
inorderIndexDict = {ch : i for i, ch in enumerate(inorder)}
self.rootIndex = len(postorder) - 1
def solve(l, r):
if l > r: return None
root = TreeNode(postorder[self.rootIndex])
... | construct-binary-tree-from-inorder-and-postorder-traversal | [Python3] O(n) Time, O(1) Space Solution faster than 95% | samirpaul1 | 3 | 159 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 707 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/2242511/Python3-Solving-3-binary-tree-construction-problems-using-same-template | class Solution:
def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
if not preorder or not postorder:
return
root = TreeNode(preorder[0])
if len(preorder) == 1:
return root
index = postorder.index(preorder[... | construct-binary-tree-from-inorder-and-postorder-traversal | [Python3] Solving 3 binary tree construction problems using same template | Gp05 | 1 | 27 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 708 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/2242511/Python3-Solving-3-binary-tree-construction-problems-using-same-template | class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if not preorder or not inorder:
return
root = TreeNode(preorder[0])
mid = inorder.index(preorder[0])
root.left = self.buildTree(preorder[1: mid+1], inorder[:mid])
... | construct-binary-tree-from-inorder-and-postorder-traversal | [Python3] Solving 3 binary tree construction problems using same template | Gp05 | 1 | 27 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 709 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/2242511/Python3-Solving-3-binary-tree-construction-problems-using-same-template | class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
if not postorder or not inorder:
return
root = TreeNode(postorder[-1])
mid = inorder.index(postorder[-1])
root.left = self.buildTree(inorder[:mid], postorder[:mid... | construct-binary-tree-from-inorder-and-postorder-traversal | [Python3] Solving 3 binary tree construction problems using same template | Gp05 | 1 | 27 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 710 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/2225038/Index-Recursion-approach-or-Python | class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
def util(inorder,postorder,n):
if not inorder or not postorder:
return None
root = TreeNode(postorder[-1])
mid = inorder.index(postorder[-1])
r... | construct-binary-tree-from-inorder-and-postorder-traversal | Index Recursion approach | Python | bliqlegend | 1 | 51 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 711 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/1266043/Python3-simple-solution-using-recursion | class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
def build(a, b, c, postorder, inorder):
if a < 0 or b > c:
return
root = TreeNode(postorder[a])
x = inorder.index(postorder[a],b,c+1)
root.left = build(a... | construct-binary-tree-from-inorder-and-postorder-traversal | Python3 simple solution using recursion | EklavyaJoshi | 1 | 64 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 712 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/691195/Python3-stack-O(N) | class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
loc = {x : i for i, x in enumerate(inorder)}
root = None
stack = []
for x in reversed(postorder):
if not root: root = node = TreeNode(x)
elif loc[node.val] < ... | construct-binary-tree-from-inorder-and-postorder-traversal | [Python3] stack O(N) | ye15 | 1 | 246 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 713 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/691195/Python3-stack-O(N) | class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
mp = {v: i for i, v in enumerate(inorder)} #mapping from val to pos
def fn(lo, hi):
"""Return sub-tree root from inorder[lo:hi]"""
if lo == hi: return None
mid = m... | construct-binary-tree-from-inorder-and-postorder-traversal | [Python3] stack O(N) | ye15 | 1 | 246 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 714 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/2770905/Python-Sol.-Faster-then-97.78 | class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
dic = {}
for idx, val in enumerate(inorder):
dic[val] = idx
def f(start, end):
if start > end:
return None
node = TreeNod... | construct-binary-tree-from-inorder-and-postorder-traversal | Python Sol. Faster then 97.78% | pranjalmishra334 | 0 | 9 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 715 |
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/1517559/Python-DFS-O(n)-O(n) | class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
m = {}
for i in range(len(inorder)):
m[inorder[i]] = i
def helper(in_l, in_r, inorder, po_l, po_r, postorder):
if po_l >= po_r:
return Tre... | construct-binary-tree-from-inorder-and-postorder-traversal | Python DFS O(n), O(n) | Kenfunkyourmama | 0 | 78 | construct binary tree from inorder and postorder traversal | 106 | 0.575 | Medium | 716 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/359962/Python-recursive-and-iterative | class Solution:
def helper(self, result, depth, node):
if not node:
return
if len(result) < depth:
result.append([])
result[depth-1].append(node.val)
self.helper(result, depth+1, node.left)
self.helper(result, depth+1, node.right)... | binary-tree-level-order-traversal-ii | Python recursive and iterative | amchoukir | 4 | 519 | binary tree level order traversal ii | 107 | 0.604 | Medium | 717 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/690555/Python3-level-order-traversal | class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
ans, queue = [], [root]
while queue:
tmp, val = [], []
for node in queue:
if node:
val.append(node.val)
tmp.extend([node.left, node.right]... | binary-tree-level-order-traversal-ii | [Python3] level-order traversal | ye15 | 2 | 116 | binary tree level order traversal ii | 107 | 0.604 | Medium | 718 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/2771047/python-Sol.-Faster-then-92.55 | class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
ans=[]
q=collections.deque()
q.append(root)
while q:
l=len(q)
lvl=[]
for i in range(l):
node=q.popleft()
if node:
... | binary-tree-level-order-traversal-ii | python Sol. Faster then 92.55% | pranjalmishra334 | 0 | 2 | binary tree level order traversal ii | 107 | 0.604 | Medium | 719 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/2672990/Python | class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
result = []
if root is None:
return result
queue = [root]
while queue:
level = []
size = len(queue)
for _ in range(size):
node = queue.pop(0)
if node.left:
queue.append(n... | binary-tree-level-order-traversal-ii | Python答え | namashin | 0 | 11 | binary tree level order traversal ii | 107 | 0.604 | Medium | 720 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/2534535/Simple-Python-Solution-oror-BFS | class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
ans = []
cur_level, cur_level_nodes = 0, []
# initialise and add root in queue with level as 0
q = deque()
q.append((root, 0))
while q:
# get first element from the queue and its resp. ... | binary-tree-level-order-traversal-ii | 🔥 Simple Python Solution || BFS | wilspi | 0 | 26 | binary tree level order traversal ii | 107 | 0.604 | Medium | 721 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/2458591/super-easy-approach-using-bfs-iterative-TC%3A-O(N)-SC-O(N) | class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
res = []
if(root == None):
return res
q = deque()
q.append(root)
while(q):
sz = len(q)
curr = []
for _ in range(sz):
temp =... | binary-tree-level-order-traversal-ii | super easy approach using bfs iterative TC: O(N) SC O(N) | rajitkumarchauhan99 | 0 | 39 | binary tree level order traversal ii | 107 | 0.604 | Medium | 722 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/2333309/simple-python-bfs-solution | class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
# same bfs approach as of level order 1
# but we take res as deque, and appendleft at all the time
# in this way we get , desired result
if root is None:
return
# bf... | binary-tree-level-order-traversal-ii | simple python bfs solution | Aniket_liar07 | 0 | 15 | binary tree level order traversal ii | 107 | 0.604 | Medium | 723 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/2299998/Python3-or-BFS-Approach | class Solution: def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return None
q=deque()
q.append(root)
ans=[]
while q:
size=len(q)
temp=[]
for i in range(size):
ele=q.popleft()
... | binary-tree-level-order-traversal-ii | [Python3] | BFS Approach | swapnilsingh421 | 0 | 11 | binary tree level order traversal ii | 107 | 0.604 | Medium | 724 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/2103383/Python-using-BFS | class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return root
q = [root]
level = []
res = [[root.val]]
x = 0
while q and root:
for node in q:
if node.left:
... | binary-tree-level-order-traversal-ii | Python using BFS | ankurbhambri | 0 | 16 | binary tree level order traversal ii | 107 | 0.604 | Medium | 725 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/1991093/Python-Solution-Postorder-traversal | class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
res = []
def levelOrderBottomInner(node, level):
if node:
if len(res)==level:
res.append([])
levelOrderBottomInner(node.left, level+1)
... | binary-tree-level-order-traversal-ii | ✅ Python Solution - Postorder traversal | constantine786 | 0 | 31 | binary tree level order traversal ii | 107 | 0.604 | Medium | 726 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/1922031/Level-by-level-99-speed | class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
ans = []
level = [root] if root else []
while level:
new_level, values = [], []
for node in level:
values.append(node.val)
if node.left:
... | binary-tree-level-order-traversal-ii | Level by level, 99% speed | EvgenySH | 0 | 61 | binary tree level order traversal ii | 107 | 0.604 | Medium | 727 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/1569226/python3-solution-using-queue | class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
self.res = [[root.val]]
q = [root]
while q:
z = []
x = []
n = len(q)
for i in range(n):
node = q... | binary-tree-level-order-traversal-ii | python3 solution using queue | EklavyaJoshi | 0 | 18 | binary tree level order traversal ii | 107 | 0.604 | Medium | 728 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/1399697/Bottom-up-BFS-in-Python-3-using-generator-without-extra-module-in-6-lines | class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
def reversed_bfs(level: List[TreeNode]):
if not level:
return []
yield from reversed_bfs([c for n in level for c in (n.left, n.right) if c])
yield [n.val for n in leve... | binary-tree-level-order-traversal-ii | Bottom-up BFS in Python 3, using generator without extra module in 6 lines | mousun224 | 0 | 58 | binary tree level order traversal ii | 107 | 0.604 | Medium | 729 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/1368871/Python-BFS | class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
if not root: return []
# queue contains (node, level)
q = collections.deque([(root, 0)])
# key=row, value=list of nodes in row
ans = collections.defaultdict(list)
# BFS
whi... | binary-tree-level-order-traversal-ii | Python BFS | SmittyWerbenjagermanjensen | 0 | 59 | binary tree level order traversal ii | 107 | 0.604 | Medium | 730 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/1257055/Python3-BFS | class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
queue = [root]
level = []
next_level = []
res = []
while queue:
for node in queue:
level.append(node.val)
... | binary-tree-level-order-traversal-ii | Python3 BFS | rahulkp220 | 0 | 35 | binary tree level order traversal ii | 107 | 0.604 | Medium | 731 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/1237086/Easy-Python-Solution-with-BFS | class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if root is None: # Base case
return []
result = [[root.val]] # result starts with root node value
queue = [root] # Queue starts with root node
while queue:
level = [] ... | binary-tree-level-order-traversal-ii | Easy Python Solution with BFS | iamrafiul | 0 | 47 | binary tree level order traversal ii | 107 | 0.604 | Medium | 732 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/1159297/Python3-solution-(BFS) | class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
result: List[List[int]] = []
if not root:
return result
queue: Deque[TreeNode] = deque([root])
count: int = 0
while queue:
result.append([... | binary-tree-level-order-traversal-ii | Python3 solution (BFS) | alexforcode | 0 | 30 | binary tree level order traversal ii | 107 | 0.604 | Medium | 733 |
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/976974/python-BFS-simple-to-understand-with-Queue | class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
if root is None: return
queue = deque([root])
op = []
while queue:
level = []
for i in range(len(queue)):
node = queue.popleft()
level.append(node.va... | binary-tree-level-order-traversal-ii | python BFS simple to understand with Queue | vd3 | 0 | 51 | binary tree level order traversal ii | 107 | 0.604 | Medium | 734 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2428167/Easy-oror-0-ms-oror-100-oror-Fully-Explained-oror-(Java-C%2B%2B-Python-JS-C-Python3) | class Solution(object):
def sortedArrayToBST(self, nums):
# Base condition...
if len(nums) == 0:
return None
# set the middle node...
mid = len(nums)//2
# Initialise root node with value same as nums[mid]
root = TreeNode(nums[mid])
# Assign left su... | convert-sorted-array-to-binary-search-tree | Easy || 0 ms || 100% || Fully Explained || (Java, C++, Python, JS, C, Python3) | PratikSen07 | 19 | 900 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 735 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2428167/Easy-oror-0-ms-oror-100-oror-Fully-Explained-oror-(Java-C%2B%2B-Python-JS-C-Python3) | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
# Base condition...
if len(nums) == 0:
return None
# set the middle node...
mid = len(nums)//2
# Initialise root node with value same as nums[mid]
root = TreeNode(nums[mid])... | convert-sorted-array-to-binary-search-tree | Easy || 0 ms || 100% || Fully Explained || (Java, C++, Python, JS, C, Python3) | PratikSen07 | 19 | 900 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 736 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/1363643/Python3-Two-Solutions-Explained-with-Diagrams | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
# base case
if not nums: return None
# getting the mid
mid = len(nums)//2
node = TreeNode(nums[mid])
# left node is given the responsibility till mid,
# but not includi... | convert-sorted-array-to-binary-search-tree | [Python3] Two Solutions Explained with Diagrams | chaudhary1337 | 13 | 833 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 737 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/1363643/Python3-Two-Solutions-Explained-with-Diagrams | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
def recurse(l, r):
# base case, l must always be <= r
# l == r is the case of a leaf node.
if l > r: return None
mid = (l+r)//2
node = TreeNode(nums[mid])
node.lef... | convert-sorted-array-to-binary-search-tree | [Python3] Two Solutions Explained with Diagrams | chaudhary1337 | 13 | 833 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 738 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2405607/Python-Elegant-and-Short-or-O(n)-time-and-O(log(n))-space | class Solution:
"""
Time: O(n)
Memory: O(log(n))
"""
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
return self._construct(0, len(nums) - 1, nums)
@classmethod
def _construct(cls, left: int, right: int, nums: List[int]) -> Optional[TreeNode]:
if left <= right:
mid = (left + right) ... | convert-sorted-array-to-binary-search-tree | Python Elegant & Short | O(n) time & O(log(n)) space | Kyrylo-Ktl | 2 | 78 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 739 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/1364345/Python-or-97-beats-python-solution-or-Easy | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
if not nums: return None
# Find the middle point which you can define as root
middle = (len(nums)//2)
root = TreeNode(nums[middle])
root.left = self.sortedArrayToBST(nums[:middle])
... | convert-sorted-array-to-binary-search-tree | Python | 97% beats python solution | Easy | vsahoo | 2 | 190 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 740 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/361353/Python-recursive-and-iterative-solution | class Solution:
def helper(self, nums, start, stop):
if (stop < start or start >= stop):
return None
mid = start + (stop - start)//2
node = TreeNode(nums[mid])
node.left = self.helper(nums, start, mid)
node.right = self.helper(nums, mid+1, stop)
retur... | convert-sorted-array-to-binary-search-tree | Python recursive and iterative solution | amchoukir | 2 | 593 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 741 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/313180/Simple-python-recursive-solution | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
if not nums:return
mid = len(nums)//2
root = TreeNode(nums[mid])
root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums[mid+1:])
return root | convert-sorted-array-to-binary-search-tree | Simple python recursive solution | aj_to_rescue | 2 | 299 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 742 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2407334/Easy-Python-Solution | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def bst(l,h):
if l>h:
return None
mid=(l+h)//2
root=TreeNode(nums[mid])
root.left=bst(l,mid-1)
root.right=bst(mid+1,h)
return root
... | convert-sorted-array-to-binary-search-tree | Easy Python Solution | a_dityamishra | 1 | 43 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 743 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/1782686/python3-solution | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def dfs(left,right):
if left>right:
return None
mid=(left+right+1)//2
root=TreeNode(nums[mid])
root.left=dfs(left,mid-1)
... | convert-sorted-array-to-binary-search-tree | python3 solution | Karna61814 | 1 | 89 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 744 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/1580064/Python3-two-liner-99 | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not (l := len(nums)): return None
return TreeNode(nums[l // 2], self.sortedArrayToBST(nums[:l // 2]), self.sortedArrayToBST(nums[l // 2 + 1:])) | convert-sorted-array-to-binary-search-tree | Python3 two liner, 99% | DamonHolland | 1 | 232 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 745 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/1440486/97-FAST!!-or-PYTHON3-or-RECURSIVE-SOLUTION | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def buildBST(nums, start, end) :
#Edge Case
if start > end :
return None
#Creating a new node
newNode = TreeNode()
if start == end :
newNode.val = nums[start]
newNode.left = None
newNode.ri... | convert-sorted-array-to-binary-search-tree | 97% FAST!! | PYTHON3 | RECURSIVE SOLUTION | athrvb | 1 | 141 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 746 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/1106999/Python3-simple-solution-faster-than-99-users-and-lesser-memory-than-100-users | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
def bst(x):
if len(x) <= 0:
return
mid = len(x)//2
node = TreeNode(x[mid])
node.left = bst(x[:mid])
node.right = bst(x[mid+1:])
return node
... | convert-sorted-array-to-binary-search-tree | Python3 simple solution faster than 99% users and lesser memory than 100% users | EklavyaJoshi | 1 | 160 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 747 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2781137/Python-3-or-Very-easy-or-beats-84-space | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if len(nums) == 0:
return
middle = len(nums) // 2
root = TreeNode(nums[middle])
root.left = self.sortedArrayToBST(nums[:middle])
root.right = self.sortedArrayToBST(nums[m... | convert-sorted-array-to-binary-search-tree | Python 3 | Very easy | beats 84% space | niccolosottile | 0 | 4 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 748 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2503596/faster-than-99-one-liner-%2B-simplified-python-solution | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums:
return None
root_i = len(nums)//2
root = TreeNode(val = nums[root_i])
root.left = self.sortedArrayToBST(nums[0:root_i])
root.right = self.sortedArrayToBST(nums[root_i+1:])
... | convert-sorted-array-to-binary-search-tree | faster than 99% one liner + simplified python solution | Sivle | 0 | 43 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 749 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2408097/4-line-Python-Easy-Solution | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
n=len(nums)
if not nums:
return None
mid=n//2
return TreeNode(nums[mid],self.sortedArrayToBST(nums[:mid]),self.sortedArrayToBST(nums[mid+1:])
) | convert-sorted-array-to-binary-search-tree | 4 line Python Easy Solution | shivangtomar | 0 | 9 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 750 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2406349/Python-Simple-Python-Solution-Using-Recursion | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def NewBST(array):
if len(array) == 0:
return None
mid_index = len(array) // 2
node = TreeNode(array[mid_index])
node.left = NewBST(array[ : mid_index])
node.right = NewBST(array[mid_index + 1 : ])
return n... | convert-sorted-array-to-binary-search-tree | [ Python ] ✅✅ Simple Python Solution Using Recursion 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 23 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 751 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2405322/Just-a-simple-recursive-solution | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def writeBST(nums, stt, edd):
if stt > edd:
return None
mid = (stt + edd) // 2
BST = TreeNode()
BST.val = nums[mid]
BST.... | convert-sorted-array-to-binary-search-tree | Just a simple recursive solution | hanzhe_ye | 0 | 2 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 752 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2404583/Python-4-lines-recursive | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums: return None
mid = len(nums) >> 1
node = TreeNode(nums[mid],
self.sortedArrayToBST(nums[:mid]),
self.sortedArrayToBST(nums[mid+1:]))
return n... | convert-sorted-array-to-binary-search-tree | Python 4 lines recursive | SmittyWerbenjagermanjensen | 0 | 28 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 753 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2404583/Python-4-lines-recursive | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums: return None
mid = len(nums) >> 1
node = TreeNode(nums[mid])
node.left, node.right = self.sortedArrayToBST(nums[:mid]), self.sortedArrayToBST(nums[mid+1:])
return node | convert-sorted-array-to-binary-search-tree | Python 4 lines recursive | SmittyWerbenjagermanjensen | 0 | 28 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 754 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2404560/Python-recursive | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums:
return None
mid = len(nums) // 2
node = TreeNode(nums[mid])
node.left = self.sortedArrayToBST(nums[:mid])
node.right = self.sortedArrayToBST(nums[mid+1:])
... | convert-sorted-array-to-binary-search-tree | Python, recursive | blue_sky5 | 0 | 8 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 755 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2308199/Python3-simple-solution | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if nums:
mid = len(nums) // 2
root = TreeNode(nums[mid])
root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums[mid+1:])
return root | convert-sorted-array-to-binary-search-tree | Python3 simple solution | mediocre-coder | 0 | 42 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 756 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2086299/simple-recursive-solution-in-python3 | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums:
return None
mid = len(nums) // 2
leftTree = self.sortedArrayToBST(nums[:mid])
node = TreeNode(nums[mid])
rightTree = self.sortedArrayToBST(nums[mid+1:])
node.le... | convert-sorted-array-to-binary-search-tree | simple recursive solution in python3 | rahulsh31 | 0 | 55 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 757 |
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/2037859/Python-runtime-65.85-memory-83.43 | class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def recur(start, end):
if end < start:
return None
mid = floor((start+end)/2)
node = TreeNode(nums[mid])
node.left = recur(start, mid-1)
node.right ... | convert-sorted-array-to-binary-search-tree | Python, runtime 65.85%, memory 83.43% | tsai00150 | 0 | 87 | convert sorted array to binary search tree | 108 | 0.692 | Easy | 758 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2767308/Python-beats-86-(recursive-solution) | class Solution:
l = 'left'
r = 'right'
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
if not head: return None
nums = []
while head:
nums.append(head.val)
head = head.next
mid = len(nums) // 2
... | convert-sorted-list-to-binary-search-tree | Python beats 86% (recursive solution) | farruhzokirov00 | 2 | 150 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 759 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/1333193/literal-recursion-in-Python | class Solution:
def sortedListToBST(self, head: ListNode) -> TreeNode:
if not head:
return head
if not head.next:
return TreeNode(head.val)
fast = slow = prev_of_slow = head
# looking for median of list
while fast and fast.next:
pr... | convert-sorted-list-to-binary-search-tree | literal recursion in Python | mousun224 | 1 | 101 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 760 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2823273/Python-or-DFS-or-Easy-to-understand | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
if not head: return None
def dfs(node):
if not node:
return None
slow = node
fast = node
prev = None
while fast and fast.next:
... | convert-sorted-list-to-binary-search-tree | Python | DFS | Easy to understand | ajay_gc | 0 | 3 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 761 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2748343/Python-solution-or-recursion | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
nums = []
while head:
nums.append(head.val)
head = head.next
def ToBST(nums):
if not nums:
return None
mid = len(nu... | convert-sorted-list-to-binary-search-tree | Python solution | recursion | maomao1010 | 0 | 3 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 762 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2697198/Python-or-DFS-or-Recursion-or-Convert-To-Array | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
if not head:
return None
nums = []
L = 0
while head is not None:
nums.append(head.val)
head = head.next
L += 1
#Convert to array first
... | convert-sorted-list-to-binary-search-tree | Python | DFS | Recursion | Convert To Array | Arana | 0 | 3 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 763 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2669583/Python-Simple-Recursive-Solution | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
# Base case
if not head:
return None
elif not head.next:
return TreeNode(head.val)
# Divide into half
slow, fast = head, head.next.next
while fast and f... | convert-sorted-list-to-binary-search-tree | Python Simple Recursive Solution | arnavn101 | 0 | 5 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 764 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2665316/Python-or-Easy-to-understand-TLE-solution-and-Optimized-solution(Faster-than-98) | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
length = 0
curr = head
while curr:
length += 1
curr = curr.next
def value(mid):
curr = head
while curr and mid >= 1:
mid -= 1
... | convert-sorted-list-to-binary-search-tree | Python | Easy to understand TLE solution and Optimized solution(Faster than 98%) | KevinJM17 | 0 | 4 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 765 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2665316/Python-or-Easy-to-understand-TLE-solution-and-Optimized-solution(Faster-than-98) | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
length = 0
curr = head
while curr:
length += 1
curr = curr.next
def BSTbuilder(start, end):
nonlocal head
if start > end:
return... | convert-sorted-list-to-binary-search-tree | Python | Easy to understand TLE solution and Optimized solution(Faster than 98%) | KevinJM17 | 0 | 4 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 766 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2662137/Python3-or-Recursive-Solution-or-O(nlogn) | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
if head==None:
return None
prev=None
fast,slow=head,head
while fast and fast.next:
fast=fast.next.next
prev=slow
slow=slow.next
if prev:p... | convert-sorted-list-to-binary-search-tree | [Python3] | Recursive Solution | O(nlogn) | swapnilsingh421 | 0 | 12 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 767 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2470711/python3-Convert-to-sorted-array-and-recursively-make-BST | class Solution:
def sortedListToBST(self, head):
sorted_array = []
curr = head
# First convert sorted array
while curr != None:
sorted_array.append(curr.val)
curr = curr.next
# Now convert to BST, here in every level parent of subtree will be mid of th... | convert-sorted-list-to-binary-search-tree | python3 - Convert to sorted array & recursively make BST | user2354hl | 0 | 26 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 768 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/2047261/Python-solution | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
# I have taken a dummy node of value 69 and we will return tree.left which is our actual root
tree = TreeNode(69)
def helper(start,tree, isLeft):
if start is None:
return
... | convert-sorted-list-to-binary-search-tree | Python solution | b160106 | 0 | 50 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 769 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/1975005/Python-O(n)-Time-O(n)-Space-Simple-Easy-to-understand | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
nums = []
while head:
nums.append(head.val)
head = head.next
def dfs(i,j,nums):
if i > j:
return None
mid = (i+j)//2
node = T... | convert-sorted-list-to-binary-search-tree | Python O(n) Time O(n) Space Simple Easy to understand | Sameer177 | 0 | 87 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 770 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/1917076/Python-or-Convert-to-List-then-Recursion | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
out = []
while head:
out.append(head.val)
head = head.next
def helper(out):
if not out:
return None
... | convert-sorted-list-to-binary-search-tree | [Python] | Convert to List then Recursion | i_architect | 0 | 65 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 771 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/1782688/python3-solution-using-two-pointers-method | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
if head==None:
return None
if head.next==None:
return TreeNode(head.val)
slow=head
fast=head
prev=None
while fast and fast.next:
... | convert-sorted-list-to-binary-search-tree | python3 solution using two pointers method | Karna61814 | 0 | 65 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 772 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/1573064/Thanks-for-Helping | class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
def insertNode(node):
if not node: return node
if not node.next: return TreeNode(node.val)
fast = slow = node
while fast and fast.next:
slow = slow.next
... | convert-sorted-list-to-binary-search-tree | Thanks for Helping | siddp6 | 0 | 55 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 773 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/1495961/Python3-O(1)-space-solution | class Solution:
def build_sub_tree(self, head):
if not head:
return None
if not head.next:
return TreeNode(head.val)
fast = slow = head
left_tail = None
while fast and fast.next:
fast = fast.next.next
... | convert-sorted-list-to-binary-search-tree | [Python3] O(1) space solution | maosipov11 | 0 | 108 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 774 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/1313423/Easy-Readable-O(n)-time-and-space-solution | class Solution:
def sortedListToBST(self, head: ListNode) -> TreeNode:
array = self.findValues(head)
return self.constructBST(array, 0, len(array)-1)
def constructBST(self, array, left, right):
if left <= right:
mid = (left+right)//2
root = TreeNode(array[mid])
... | convert-sorted-list-to-binary-search-tree | Easy Readable O(n) time and space solution | ramit_kumar | 0 | 136 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 775 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/693850/Python3-two-approaches | class Solution:
def sortedListToBST(self, head: ListNode) -> TreeNode:
nums = []
while head:
nums.append(head.val)
head = head.next
def fn(lo, hi):
"""Return node constructed from nums[lo:hi]"""
if lo == hi: return None
... | convert-sorted-list-to-binary-search-tree | [Python3] two approaches | ye15 | 0 | 49 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 776 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/693850/Python3-two-approaches | class Solution:
def sortedListToBST(self, head: ListNode) -> TreeNode:
node, n = head, 0
while node: node, n = node.next, n+1
def fn(lo, hi, node):
"""Return root of tree using nodes from lo (inclusive) to hi (exclusive)"""
if lo == hi: return None, node... | convert-sorted-list-to-binary-search-tree | [Python3] two approaches | ye15 | 0 | 49 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 777 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/691136/Python3-recursive-solution | class Solution:
def sortedListToBST(self, head: ListNode) -> TreeNode:
nums = []
while head:
nums.append(head.val)
head = head.next
def fn(lo, hi):
"""Return root of BST constructed from nums[lo:hi]"""
if lo == hi: return None
... | convert-sorted-list-to-binary-search-tree | [Python3] recursive solution | ye15 | 0 | 35 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 778 |
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/491570/Python3%3A-Short-recursive-solution | class Solution:
def sortedListToBST(self, head: ListNode) -> TreeNode:
if not head or not head.next:
return TreeNode(head.val) if head else None
slow, fast = head, head.next
while fast.next and fast.next.next:
slow, fast = slow.next, fast.next.next
fast, s... | convert-sorted-list-to-binary-search-tree | Python3: Short, recursive solution | andnik | 0 | 195 | convert sorted list to binary search tree | 109 | 0.574 | Medium | 779 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2428871/Very-Easy-oror-100-oror-Fully-Explained-(C%2B%2B-Java-Python-JavaScript-Python3) | class Solution(object):
def isBalanced(self, root):
return (self.Height(root) >= 0)
def Height(self, root):
if root is None: return 0
leftheight, rightheight = self.Height(root.left), self.Height(root.right)
if leftheight < 0 or rightheight < 0 or abs(leftheight - rightheight) >... | balanced-binary-tree | Very Easy || 100% || Fully Explained (C++, Java, Python, JavaScript, Python3) | PratikSen07 | 83 | 6,700 | balanced binary tree | 110 | 0.483 | Easy | 780 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2428871/Very-Easy-oror-100-oror-Fully-Explained-(C%2B%2B-Java-Python-JavaScript-Python3) | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
return (self.Height(root) >= 0)
def Height(self, root: Optional[TreeNode]) -> bool:
if root is None: return 0
leftheight, rightheight = self.Height(root.left), self.Height(root.right)
if leftheight < 0 or ri... | balanced-binary-tree | Very Easy || 100% || Fully Explained (C++, Java, Python, JavaScript, Python3) | PratikSen07 | 83 | 6,700 | balanced binary tree | 110 | 0.483 | Easy | 781 |
https://leetcode.com/problems/balanced-binary-tree/discuss/982527/Simple-Python-Recursive-Solution | class Solution:
def isBalanced(self, root: TreeNode) -> bool:
def getDepth(node):
if not node:
return 0
return 1 + max(getDepth(node.left), getDepth(node.right))
if not root:
return True
return abs(getDepth(root.left) - getDepth(ro... | balanced-binary-tree | Simple Python Recursive Solution | KevinZzz666 | 11 | 1,600 | balanced binary tree | 110 | 0.483 | Easy | 782 |
https://leetcode.com/problems/balanced-binary-tree/discuss/611324/python-83-100-solution | class Solution:
def is_leaf(self, root: TreeNode) -> bool:
return not root.left and not root.right
def isBalanced(self, root: TreeNode) -> bool:
if not root:
return True
if self.is_leaf(root):
root.height = 1
return True
elif not root.left a... | balanced-binary-tree | python 83%, 100% solution | usualwitch | 6 | 1,300 | balanced binary tree | 110 | 0.483 | Easy | 783 |
https://leetcode.com/problems/balanced-binary-tree/discuss/1626310/Python-easy-and-simple-to-understand-solution | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if root is None:
return True
hleft=self.findMaxheight(root.left)
hright=self.findMaxheight(root.right)
if abs(hleft-hright)>1:
return False
if s... | balanced-binary-tree | Python easy and simple to understand solution | diksha_choudhary | 4 | 386 | balanced binary tree | 110 | 0.483 | Easy | 784 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2580390/Easy-solution-Must-Check | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if not root: return 1
leftHeight = self.isBalanced(root.left)
if leftHeight == 0: return 0
rightHeight = self.isBalanced(root.right)
if rightHeight == 0: return 0
... | balanced-binary-tree | Easy solution [Must Check] | lokeshsenthilkumar | 2 | 244 | balanced binary tree | 110 | 0.483 | Easy | 785 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2159204/Easy-6-line-python3-code | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def dfs(root):
if not root:
return [True, 0]
left, right = dfs(root.left), dfs(root.right)
return [(left[0] and right[0] and abs(left[1] - right[1]) <= 1), 1 + max(left[1], right[1])]
return dfs(root)[0] | balanced-binary-tree | Easy 6 line python3 code | sagarhasan273 | 2 | 69 | balanced binary tree | 110 | 0.483 | Easy | 786 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2159141/Python-oror-DFS | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
res = [0]
def dfs(root):
if not root:
return 0
left = dfs(root.left)
right = dfs(root.right)
res[0] = max(res[0], abs(left - right))
return 1 + max(left, right)
dfs(root)
return True if res[0] <=1 else False | balanced-binary-tree | Python || DFS | sagarhasan273 | 2 | 87 | balanced binary tree | 110 | 0.483 | Easy | 787 |
https://leetcode.com/problems/balanced-binary-tree/discuss/1237889/Python-or-Time-Complexity-O(n)-or-Faster-than-99.56-or-Simple-and-Clean-or-Space-Complexity-O(1)-or | class Solution:
def isBalanced(self, root: TreeNode) -> bool:
# If root is empty
if root==None:
return True
# Building a utility function to calculate if node is a leaf
def is_leaf(node):
if node.left == None and node.right==None:
... | balanced-binary-tree | Python | Time Complexity O(n) | Faster than 99.56% | Simple & Clean | Space Complexity O(1) | | shuklaeshita0209 | 2 | 423 | balanced binary tree | 110 | 0.483 | Easy | 788 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2619367/Simple-Python-Solution-or-DFS | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def dfs(root):
if not root:
return [True, 0]
left, right = dfs(root.left), dfs(root.right)
balance = (left[0] and right[0] and abs(left[1] - right[1]) <= 1)
return [balance, 1 + max(left[1], right[1])]
res = dfs(root)
retur... | balanced-binary-tree | Simple Python Solution | DFS | nikhitamore | 1 | 116 | balanced binary tree | 110 | 0.483 | Easy | 789 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2404832/Simple-Python-Solution | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if root is None:
return True
def height(root):
if not root:
return 0
return 1 + max(height(root.left), height(root.right))
left,right=height(root.left),height(root.rig... | balanced-binary-tree | Simple Python Solution | Siddharth_singh | 1 | 121 | balanced binary tree | 110 | 0.483 | Easy | 790 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2029411/Python-oror-Simple-Iterative-BFS-using-Deque | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if not root: return True
def height(root):
if not root: return -1
h , Q = 0, deque([[root]])
while Q:
level, new_level = Q.popleft(), []
for node in level:
if node.right: new_level.append(node.right)
if node.left : ... | balanced-binary-tree | Python || Simple Iterative BFS using Deque | morpheusdurden | 1 | 201 | balanced binary tree | 110 | 0.483 | Easy | 791 |
https://leetcode.com/problems/balanced-binary-tree/discuss/1990709/Python-Iterative-or-89.93-Runtime-or-98.57-Memory | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
dummy = TreeNode()
stack = [[dummy] + [0 for _ in range(5)],[root, 0, False, 0, 0]]
while len(stack) > 1:
cur, kidsSeen, is_left, left_height, right_height = stack... | balanced-binary-tree | Python Iterative | 89.93 % Runtime | 98.57 % Memory | neejweej | 1 | 216 | balanced binary tree | 110 | 0.483 | Easy | 792 |
https://leetcode.com/problems/balanced-binary-tree/discuss/1696839/Python-Recursive-Solution-or-Easy-to-read | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
def getDepth(node):
if not node:
return 0
leftDepth = getDepth(node.left)
rightDepth = getDepth(node.right)
... | balanced-binary-tree | Python Recursive Solution | Easy to read | fourcommas | 1 | 241 | balanced binary tree | 110 | 0.483 | Easy | 793 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2775220/Python3-or-56-ms-or-Beats-91.7-or-Recursion | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def recursive(node: TreeNode, curr_depth: Optional[int] = 0) -> int:
curr_depth += 1
if not(node.left) and not(node.right): return curr_depth
left_depth = right_depth = curr_depth
if n... | balanced-binary-tree | Python3 | 56 ms | Beats 91.7% | Recursion | thomwebb | 0 | 15 | balanced binary tree | 110 | 0.483 | Easy | 794 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2713199/Python3-Easy-Solution | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
res = [0]
def helper(node):
if not node:
return 0
left = helper(node.left)
right = helper(node.right)
res[0] = max(res[0],abs(lef... | balanced-binary-tree | Python3 Easy Solution | shashank732001 | 0 | 9 | balanced binary tree | 110 | 0.483 | Easy | 795 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2697106/Simplest-Python-Solution-with-tree-depth-diff | class Solution:
def isBalanced(self, root):
if root == None:
return True
l = self.depth(root.left) # get depth of left side of the tree
r = self.depth(root.right) # get depth of right side of the tree
# check the difference
if abs(l - r) <= 1:
... | balanced-binary-tree | Simplest Python Solution with tree depth diff | abhaynayak24 | 0 | 11 | balanced binary tree | 110 | 0.483 | Easy | 796 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2697106/Simplest-Python-Solution-with-tree-depth-diff | class Solution:
def isBalanced(self, root):
if root == None:
return True
# check the difference and recursively keep checking further down
return abs( self.depth(root.left) - self.depth(root.right) ) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)
... | balanced-binary-tree | Simplest Python Solution with tree depth diff | abhaynayak24 | 0 | 11 | balanced binary tree | 110 | 0.483 | Easy | 797 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2675017/easy-understand%3A-combination-of-previous-2-problems(94-and-104) | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
#check if root is []
if not root:
return True
#Problem: 104.Maximum Depth of Binary Tree
def maxDepth(root: Optional[TreeNode]):
return 0 if not root else 1+max(maxDepth(root.left), maxDepth(root.righ... | balanced-binary-tree | easy-understand: combination of previous 2 problems(94 & 104) | Han-Fu | 0 | 36 | balanced binary tree | 110 | 0.483 | Easy | 798 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2552489/Python3-simple-approach-faster-than-94 | class Solution:
def check(self, node) -> int:
if node==None:
return 0
lh = self.check(node.left)
if lh == -1:
return -1
rh = self.check(node.right)
if rh == -1:
return -1
if abs(rh-lh) > 1:
return -1
return 1+max... | balanced-binary-tree | Python3 simple approach faster than 94% | sumedha19129 | 0 | 57 | balanced binary tree | 110 | 0.483 | Easy | 799 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.