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/balanced-binary-tree/discuss/2490619/Python-Solution-using-recursion | class Solution:
def getHeight(self,root,h):
if root is None:
return h
return max(self.getHeight(root.left,h+1),self.getHeight(root.right,h+1))
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if root is None:
return True
if root:
... | balanced-binary-tree | Python Solution [using recursion] | miyachan | 0 | 48 | balanced binary tree | 110 | 0.483 | Easy | 800 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2409309/Simple-recursive-solution-or-Python-or-O(n)-or-DFS | class Solution:
def isHeightBalanced(self, root):
if root:
left_height, left_balanced = self.isHeightBalanced(root.left)
right_height, right_balanced = self.isHeightBalanced(root.right)
return (
max(left_height, right_height) + 1,
(
... | balanced-binary-tree | Simple recursive solution | Python | O(n) | DFS | wilspi | 0 | 156 | balanced binary tree | 110 | 0.483 | Easy | 801 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2407627/SIMPLE-96.45-Faster-Python-recursive | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
heightDict = {}
if not root:
return True
def checkBalance(root: TreeNode) -> bool:
leftHeight = 0
rightHeight = 0
if root.left:
... | balanced-binary-tree | SIMPLE 96.45% Faster, Python - recursive | anhduong275 | 0 | 117 | balanced binary tree | 110 | 0.483 | Easy | 802 |
https://leetcode.com/problems/balanced-binary-tree/discuss/2041544/Python-runtime-60.80-memory-29.46 | class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
def recur(node, level):
if not node:
return level-1
left = recur(node.left, level+1)
right = recur(node.right, level+1)
... | balanced-binary-tree | Python, runtime 60.80%, memory 29.46% | tsai00150 | 0 | 167 | balanced binary tree | 110 | 0.483 | Easy | 803 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2429057/Very-Easy-oror-100-oror-Fully-Explained-(C%2B%2B-Java-Python-JS-C-Python3) | class Solution(object):
def minDepth(self, root):
# Base case...
# If the subtree is empty i.e. root is NULL, return depth as 0...
if root is None: return 0
# Initialize the depth of two subtrees...
leftDepth = self.minDepth(root.left)
rightDepth = self.minDepth(root... | minimum-depth-of-binary-tree | Very Easy || 100% || Fully Explained (C++, Java, Python, JS, C, Python3) | PratikSen07 | 32 | 2,200 | minimum depth of binary tree | 111 | 0.437 | Easy | 804 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2429057/Very-Easy-oror-100-oror-Fully-Explained-(C%2B%2B-Java-Python-JS-C-Python3) | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
# Base case...
# If the subtree is empty i.e. root is NULL, return depth as 0...
if root is None: return 0
# Initialize the depth of two subtrees...
leftDepth = self.minDepth(root.left)
rightDepth =... | minimum-depth-of-binary-tree | Very Easy || 100% || Fully Explained (C++, Java, Python, JS, C, Python3) | PratikSen07 | 32 | 2,200 | minimum depth of binary tree | 111 | 0.437 | Easy | 805 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/629657/Python-O(n)-by-DFS-and-BFS-85%2B-w-Comment | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
# base case for empty node or empty tree
return 0
if not root.left and not root.right:
# leaf node
return 1
elif not root.right:
# only has left sub-tree
... | minimum-depth-of-binary-tree | Python O(n) by DFS and BFS 85%+ [w/ Comment] | brianchiang_tw | 16 | 2,200 | minimum depth of binary tree | 111 | 0.437 | Easy | 806 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/1467207/Python3-oror-Recursive-oror-Self-Understandable-oror-Easy-Understanding | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
def recurse(root):
if root.left is None and root.right is None:
return 1
if root.left is None and root.right:
return 1+recurse(root.right)
if root.left and root.right is ... | minimum-depth-of-binary-tree | Python3 || Recursive || Self-Understandable || Easy-Understanding | bug_buster | 14 | 684 | minimum depth of binary tree | 111 | 0.437 | Easy | 807 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/1147375/Python-Easy-to-understand-DFS-logic-with-Comments | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0 # return 0 as depth if empty tree
elif not root.left and not root.right:
return 1 # base case handling
elif not root.left:
return self.minDepth(root.right)+1 # if no left chi... | minimum-depth-of-binary-tree | Python Easy to understand DFS logic with Comments | shootingstaradil | 8 | 479 | minimum depth of binary tree | 111 | 0.437 | Easy | 808 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/485658/Python-3-(seven-lines)-(beats-~95)-(BFS) | class Solution:
def minDepth(self, R: TreeNode) -> int:
if not R: return 0
N, A, d = [R], [], 1
while 1:
for n in N:
if n.left is n.right: return d
n.left and A.append(n.left), n.right and A.append(n.right)
N, A, d = A, [], d + 1
... | minimum-depth-of-binary-tree | Python 3 (seven lines) (beats ~95%) (BFS) | junaidmansuri | 3 | 783 | minimum depth of binary tree | 111 | 0.437 | Easy | 809 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2632384/Python-Elegant-and-Short-or-DFS | class Solution:
"""
Time: O(n)
Memory: O(n)
"""
def minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
left_depth = self.minDepth(root.left)
right_depth = self.minDepth(root.right)
return 1 + (
min(left_depth, right... | minimum-depth-of-binary-tree | Python Elegant & Short | DFS | Kyrylo-Ktl | 2 | 242 | minimum depth of binary tree | 111 | 0.437 | Easy | 810 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2600509/Easy-recursive-Python-solution | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root: return 0
if not root.left and not root.right: return 1
left=self.minDepth(root.left) if root.left else float('inf')
right=self.minDepth(root.right) if root.right else float('inf')
... | minimum-depth-of-binary-tree | Easy recursive Python 🐍 solution | InjySarhan | 2 | 248 | minimum depth of binary tree | 111 | 0.437 | Easy | 811 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/1768389/Python-Easiest-Approach | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
elif root.left is None and root.right is None:
return 1
elif root.left is None:
return self.minDepth(root.right)+1
elif root.right is None:
r... | minimum-depth-of-binary-tree | Python Easiest Approach | karansinghsnp | 2 | 168 | minimum depth of binary tree | 111 | 0.437 | Easy | 812 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/1234702/Python-Easiest-to-read-BFS | class Solution:
def minDepth(self, root: TreeNode) -> int:
# Readable BFS
# T: O(N)
# S: O(N)
if not root:
return 0
if not root.left and not root.right:
return 1
queue = [root]
depth = 1
while queue:
for _ in ... | minimum-depth-of-binary-tree | Python Easiest to read BFS | treksis | 2 | 218 | minimum depth of binary tree | 111 | 0.437 | Easy | 813 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/905936/Python-BFS-and-DFS-Explained-(video-%2B-code) | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
q = collections.deque()
q.append((root, 1))
while q:
node, depth = q.popleft()
if not node.left and not node.right:
... | minimum-depth-of-binary-tree | Python BFS and DFS Explained (video + code) | spec_he123 | 2 | 167 | minimum depth of binary tree | 111 | 0.437 | Easy | 814 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/905936/Python-BFS-and-DFS-Explained-(video-%2B-code) | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
if not root.left and not root.right:
return 1
if not root.right and root.left:
return 1 + self.minDepth(root.left)
if not root.left and root... | minimum-depth-of-binary-tree | Python BFS and DFS Explained (video + code) | spec_he123 | 2 | 167 | minimum depth of binary tree | 111 | 0.437 | Easy | 815 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2336115/Python3-or-O-(n)-space-or-O-(n)-time-or-BFS-or-Optimal-Solution | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
queue = deque([root])
currentLevel = 1
while queue:
currentLevelSize = len(queue)
for _ in range(currentLevelSize):
currentNode = queue.pople... | minimum-depth-of-binary-tree | Python3 | O (n) space | O (n) time | BFS | Optimal Solution | blessinto | 1 | 122 | minimum depth of binary tree | 111 | 0.437 | Easy | 816 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2323497/Python-recursive-code-intuitive-solution-with-explanation | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
# just a slight modification , in case both left andd right exist, we simply find min
# if only left exist , we find left
# if only right exist, we find right
if root is None:
return 0
left=self.... | minimum-depth-of-binary-tree | Python recursive code, intuitive solution with explanation | Aniket_liar07 | 1 | 139 | minimum depth of binary tree | 111 | 0.437 | Easy | 817 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2144542/Plain-Simple-BFS-Python-or-92 | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
q = [root]
depth = 0
while len(q) !=0:
q_len = len(q)
depth+=1
for _ in range(q_len):
curr = q.pop(0)
if curr... | minimum-depth-of-binary-tree | Plain Simple BFS Python | 92% | bliqlegend | 1 | 87 | minimum depth of binary tree | 111 | 0.437 | Easy | 818 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/1544403/Python3-BFS-beats-99.1Runtime-and-85.87Memory | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if root == None:
return 0
queue = [root]
depth = 0
while queue:
depth = depth + 1
for i in range(len(queue)):
node=queue.pop(0)
if node.left == Non... | minimum-depth-of-binary-tree | Python3 BFS beats 99.1%Runtime and 85.87%Memory | 17pchaloori | 1 | 226 | minimum depth of binary tree | 111 | 0.437 | Easy | 819 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/1207164/PythonPython3-Minimum-Depth-of-Binary-Tree | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root: return 0
if (not root.left) and (not root.right):
return 1
h = 1
minimum = float('inf')
def calc_minDepth(node, h, minimum):
if h > minimum:... | minimum-depth-of-binary-tree | [Python/Python3] Minimum Depth of Binary Tree | newborncoder | 1 | 691 | minimum depth of binary tree | 111 | 0.437 | Easy | 820 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/1156099/Python-Iterative-DFS-with-Optimization | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
stack = [(root, 1)]
minDepth = float('inf')
while stack:
node, depth = stack.pop()
if depth >= minDepth:
continue
... | minimum-depth-of-binary-tree | Python Iterative DFS with Optimization | genefever | 1 | 113 | minimum depth of binary tree | 111 | 0.437 | Easy | 821 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/692332/Python3-concise-solutions | class Solution:
def minDepth(self, root: TreeNode) -> int:
def fn(node):
"""Return minimum depth of given node"""
if not node: return 0
if not node.left or not node.right: return 1 + fn(node.left) + fn(node.right)
return 1 + min(fn(node.left), fn(node... | minimum-depth-of-binary-tree | [Python3] concise solutions | ye15 | 1 | 61 | minimum depth of binary tree | 111 | 0.437 | Easy | 822 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/692332/Python3-concise-solutions | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root: return 0
ans = self.minDepth(root.left), self.minDepth(root.right)
return 1 + (min(ans) or max(ans)) | minimum-depth-of-binary-tree | [Python3] concise solutions | ye15 | 1 | 61 | minimum depth of binary tree | 111 | 0.437 | Easy | 823 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/692332/Python3-concise-solutions | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root: return 0 #edge case
queue = [(root, 1)]
for node, i in queue:
if not node.left and not node.right: return i
if node.left: queue.append((node.left, i+1))
if node.right: queue.append((n... | minimum-depth-of-binary-tree | [Python3] concise solutions | ye15 | 1 | 61 | minimum depth of binary tree | 111 | 0.437 | Easy | 824 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/692332/Python3-concise-solutions | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root: return 0 # edge case
ans, queue = 1, [root]
while queue: # bfs by level
newq = []
for n in queue:
if n.left is n.right is None: return ans
if n.left: n... | minimum-depth-of-binary-tree | [Python3] concise solutions | ye15 | 1 | 61 | minimum depth of binary tree | 111 | 0.437 | Easy | 825 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/313197/Python3-dfs | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:return 0
self.depth = float('Inf')
def dfs(node, level):
if not node:return
if not node.right and not node.left:self.depth = min(self.depth, level)
if node.right:
dfs(no... | minimum-depth-of-binary-tree | Python3 dfs | aj_to_rescue | 1 | 175 | minimum depth of binary tree | 111 | 0.437 | Easy | 826 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/254186/Python-Easy-to-Understand | class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
left = self.minDepth(root.left)
right = self.minDepth(root.right)
return left + right + 1 if (left == 0 or right == 0) else min(left, right) + 1 | minimum-depth-of-binary-tree | Python Easy to Understand | ccparamecium | 1 | 335 | minimum depth of binary tree | 111 | 0.437 | Easy | 827 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2831558/Python-super-simple-recursion-with-explanation | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
# root is a child of a leaf
if not root:
return 0
# root is not a leaf but has a branch missing
# so mindepth is depth of non-missing branch instead of min(0, depth)
if not root.left:
... | minimum-depth-of-binary-tree | Python super simple recursion with explanation | AlecLeetcode | 0 | 1 | minimum depth of binary tree | 111 | 0.437 | Easy | 828 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2827660/simple-python-oror-bfs | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
# if tree empty
if not root:
return 0
# queue of tuples: (node, curr depth)
q = deque([(root, 0)])
# while q not empty
while(q):
# get front of queue
cur, lvl = q.... | minimum-depth-of-binary-tree | simple python || bfs | wduf | 0 | 4 | minimum depth of binary tree | 111 | 0.437 | Easy | 829 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2811203/Intuitive-%2B-Recursive-Python-Solution | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
def bfs(root,count):
if root.left and root.right:
return min(bfs(root.left,count+1),bfs(root.right,count+1))
elif root.left and not root.right:
... | minimum-depth-of-binary-tree | Intuitive + Recursive Python Solution | khoai345678 | 0 | 3 | minimum depth of binary tree | 111 | 0.437 | Easy | 830 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2781058/Python-oror-O(N)-greaterTC-oror-O(logN)-greaterSC | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
res = 10**6
def dfs(root, d1):
nonlocal res
if not root:
return 10**6
if not root.left and not root.right:
res = min(res, d1)
return res
... | minimum-depth-of-binary-tree | Python || O(N)->TC || O(logN)->SC | ak_guy | 0 | 5 | minimum depth of binary tree | 111 | 0.437 | Easy | 831 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2766622/Python-99-Faster-Solution | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
queue = [root]
root.val = 1
while queue:
s = queue.pop(0)
if s.left:
queue.append(s.left)
s.left.val = s.val +1
... | minimum-depth-of-binary-tree | [Python] 99% Faster Solution | jiarow | 0 | 10 | minimum depth of binary tree | 111 | 0.437 | Easy | 832 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2715975/some-recursion | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
c = set()
def lol(r,x):
if r:
lol(r.left,x+1)
lol(r.right,x+1)
else:
return
if not r.left and not r.right... | minimum-depth-of-binary-tree | some recursion | hibit | 0 | 3 | minimum depth of binary tree | 111 | 0.437 | Easy | 833 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2617787/Python-currentCount-solution-O(n)-time-complexity-and-O(h)-space-complexity. | class Solution:
def minDepth(self, root):
if root == None:
return 0
def DFS(root, mn = 100001, currentCount = 0):
if root == None:
return mn
currentCount += 1
if root.left == None and root.right == Non... | minimum-depth-of-binary-tree | Python currentCount solution, O(n) time complexity and O(h) space complexity. | OsamaRakanAlMraikhat | 0 | 34 | minimum depth of binary tree | 111 | 0.437 | Easy | 834 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2499129/Python-Concise-BFS-Solution-O(logN) | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
# we should solve this using bfs, since the node
# we are trying to find is closes to the root
if root is None:
return 0
queue = [(root, 1)]
while queue:
# pop t... | minimum-depth-of-binary-tree | [Python] - Concise BFS Solution - O(logN) | Lucew | 0 | 79 | minimum depth of binary tree | 111 | 0.437 | Easy | 835 |
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/2312822/Python-or-BFS | class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
import queue
if not root:
return 0
q = queue.Queue()
q.put((1, root))
while not q.empty():
level, node = q.get()
if not node.left and not node.right:
retu... | minimum-depth-of-binary-tree | Python | BFS | Sonic0588 | 0 | 88 | minimum depth of binary tree | 111 | 0.437 | Easy | 836 |
https://leetcode.com/problems/path-sum/discuss/2658792/Python-Elegant-and-Short-or-DFS | class Solution:
"""
Time: O(n)
Memory: O(n)
"""
def hasPathSum(self, root: Optional[TreeNode], target: int) -> bool:
if root is None:
return False
if root.left is None and root.right is None:
return target == root.val
return self.hasPathSum( root.le... | path-sum | Python Elegant & Short | DFS | Kyrylo-Ktl | 10 | 422 | path sum | 112 | 0.477 | Easy | 837 |
https://leetcode.com/problems/path-sum/discuss/2432844/Very-Easy-oror-100-oror-3-Line-oror-Explained-(Java-C%2B%2B-Python-JS-C-Python3) | class Solution(object):
def hasPathSum(self, root, targetSum):
# If the tree is empty i.e. root is NULL, return false...
if root is None: return 0
# If there is only a single root node and the value of root node is equal to the targetSum...
if root.val == targetSum and (root.left is None a... | path-sum | Very Easy || 100% || 3 Line || Explained (Java, C++, Python, JS, C, Python3) | PratikSen07 | 8 | 442 | path sum | 112 | 0.477 | Easy | 838 |
https://leetcode.com/problems/path-sum/discuss/2432844/Very-Easy-oror-100-oror-3-Line-oror-Explained-(Java-C%2B%2B-Python-JS-C-Python3) | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
# If the tree is empty i.e. root is NULL, return false...
if root is None: return 0
# If there is only a single root node and the value of root node is equal to the targetSum...
if root.val == targetSum... | path-sum | Very Easy || 100% || 3 Line || Explained (Java, C++, Python, JS, C, Python3) | PratikSen07 | 8 | 442 | path sum | 112 | 0.477 | Easy | 839 |
https://leetcode.com/problems/path-sum/discuss/2658457/1-line-Python | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
return (root.val == targetSum) if (root and not root.left and not root.right) else (root and (self.hasPathSum(root.right, targetSum - root.val) or self.hasPathSum(root.left, targetSum - root.val))) | path-sum | 1-line Python | alex391a | 7 | 422 | path sum | 112 | 0.477 | Easy | 840 |
https://leetcode.com/problems/path-sum/discuss/2176648/Python3-short-4lines-DFS | class Solution:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
if not root:
return False
if root.val == sum and not root.left and not root.right:
return True
return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val) | path-sum | 📌 Python3 short 4lines DFS | Dark_wolf_jss | 4 | 51 | path sum | 112 | 0.477 | Easy | 841 |
https://leetcode.com/problems/path-sum/discuss/1583269/Python3-Easy-Fast-Recursive-Solution | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root: return False
if root and not root.left and not root.right: return root.val == targetSum
return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root... | path-sum | Python3 Easy, Fast, Recursive Solution | DamonHolland | 4 | 437 | path sum | 112 | 0.477 | Easy | 842 |
https://leetcode.com/problems/path-sum/discuss/2329145/Extremely-modular-(minimal-code)-python3-solution | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root: return False
if not root.left and not root.right and root.val == targetSum: return True
return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root... | path-sum | Extremely modular (minimal code) python3 solution | byusuf | 2 | 36 | path sum | 112 | 0.477 | Easy | 843 |
https://leetcode.com/problems/path-sum/discuss/2287814/oror-Pythonoror-FASTEST-AND-SIMPLESToror-7-liner-ororrecursiveoror-Easy-to-Understand | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
def dfs(node,sum):
if not node: return False #base case for null node
if sum+node.val == targetSum and not node.left and not node.right: return True #when we find the ans
lt, rt = df... | path-sum | ✅|| Python|| FASTEST AND SIMPLEST|| 7-liner ||recursive|| Easy to Understand | HarshVardhan71 | 2 | 72 | path sum | 112 | 0.477 | Easy | 844 |
https://leetcode.com/problems/path-sum/discuss/2287814/oror-Pythonoror-FASTEST-AND-SIMPLESToror-7-liner-ororrecursiveoror-Easy-to-Understand | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
def dfs(node,sum):
if not node:return False #base case for null node
if sum+node.val==targetSum and not node.left and not node.right:return True #when we find the ans
lt=dfs(node.lef... | path-sum | ✅|| Python|| FASTEST AND SIMPLEST|| 7-liner ||recursive|| Easy to Understand | HarshVardhan71 | 2 | 72 | path sum | 112 | 0.477 | Easy | 845 |
https://leetcode.com/problems/path-sum/discuss/1658579/Python-Simple-recursive-DFS-explained | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root: return False
res = 0
def dfs(node):
if not node: return 0
nonlocal res
res += node.val
# recursively ... | path-sum | [Python] Simple recursive DFS explained | buccatini | 2 | 189 | path sum | 112 | 0.477 | Easy | 846 |
https://leetcode.com/problems/path-sum/discuss/1482571/Python-Clean-Recursive-DFS | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
def dfs(node = root, total = 0):
if not node:
return False
total += node.val
if not (node.left or node.right) and total == targetSum:
return Tr... | path-sum | [Python] Clean Recursive DFS | soma28 | 2 | 189 | path sum | 112 | 0.477 | Easy | 847 |
https://leetcode.com/problems/path-sum/discuss/2661418/Python-easy-recursion-solution | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root: return False
targetSum -= root.val
return not (targetSum or root.left or root.right) or \
self.hasPathSum(root.left, targetSum) or \
self.hasPathSum(root.right, targetSum) | path-sum | Python easy recursion solution | AgentIvan | 1 | 14 | path sum | 112 | 0.477 | Easy | 848 |
https://leetcode.com/problems/path-sum/discuss/2659426/oror-Python-easy-DFS-solution-oror | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
ans = [False]
def tree_traversal(root, curr_sum, ans):
curr_sum += root.val
if root.left is None and root.right is None:
if curr_sum == targetSum:
... | path-sum | 💯 || Python easy DFS solution || 💯 | parth_panchal_10 | 1 | 100 | path sum | 112 | 0.477 | Easy | 849 |
https://leetcode.com/problems/path-sum/discuss/2658490/Python-Iterative-DFS-oror-Faster-than-99.90 | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root: return False
stack = [[root, root.val]]
while stack:
node, total = stack.pop()
if total == targetSum and node.left is None and node.right is None: return True
... | path-sum | [Python] Iterative DFS || Faster than 99.90% | sreenithishb | 1 | 46 | path sum | 112 | 0.477 | Easy | 850 |
https://leetcode.com/problems/path-sum/discuss/2657708/4-lines-Python-Javascript | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root: return False
targetSum -= root.val
# leaf node
if root.left is root.right: return targetSum == 0
return self.hasPathSum(root.left, targetSum) or self.hasP... | path-sum | 4 lines Python / Javascript | SmittyWerbenjagermanjensen | 1 | 54 | path sum | 112 | 0.477 | Easy | 851 |
https://leetcode.com/problems/path-sum/discuss/2484453/python-simple-solution-using-DFS | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if(not root):
return False
if(root.val == targetSum and root.left == None and root.right == None):
return True
return self.hasPathSum(root.left, targetSum - root.val) or self.has... | path-sum | python simple solution using DFS | rajitkumarchauhan99 | 1 | 48 | path sum | 112 | 0.477 | Easy | 852 |
https://leetcode.com/problems/path-sum/discuss/1923955/Python-Fast-Recursive-Solution | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root:
return False
flag = [False]
def dfs(tree, num):
if not tree:
return
if tree.left is None and tr... | path-sum | Python Fast Recursive Solution | Hejita | 1 | 171 | path sum | 112 | 0.477 | Easy | 853 |
https://leetcode.com/problems/path-sum/discuss/1347938/Easy-Fast-Recursive-Python-Solution-using-DFS%2Bstack-(Faster-than-100) | class Solution:
def DFS(self,root):
if root:
self.path.append(root.val)
if not root.left and not root.right:
a = self.path.copy()
if sum(a) == self.targetSum:
self.has_sum += 1
else:
self.DFS(root.left)
... | path-sum | Easy, Fast Recursive Python Solution using DFS+stack (Faster than 100%) | the_sky_high | 1 | 168 | path sum | 112 | 0.477 | Easy | 854 |
https://leetcode.com/problems/path-sum/discuss/1235116/Python-Solutions.-Iterative-recursive-DFS | class Solution:
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
# Recursive dfs.
# Accumulate the number as you traverse the tree and compare with targetSum
# T: O(N)
# S: O(N)
return self.dfs(root, targetSum, 0)
def dfs(self, root, targetSum, total):
if not ... | path-sum | Python Solutions. Iterative, recursive DFS | treksis | 1 | 187 | path sum | 112 | 0.477 | Easy | 855 |
https://leetcode.com/problems/path-sum/discuss/865160/Python3-recursive-solution-(beats-97) | class Solution:
def hasPathSum(self, node: TreeNode, num: int) -> bool:
if node is None:
return False
if not node.right and not node.left:
return node.val == num
return self.hasPathSum(node.left, num - node.val) or self.hasPathSum(node.right, num - node.val) | path-sum | Python3 recursive solution (beats 97%) | n0execution | 1 | 269 | path sum | 112 | 0.477 | Easy | 856 |
https://leetcode.com/problems/path-sum/discuss/801559/(Python)-A-very-simple-recursive-solution | class Solution:
def hasPathSum(self, root: TreeNode, rsum: int) -> bool:
if root == None:
return False
if root.val == rsum and root.left is None and root.right is None:
return True
return (self.hasPathSum(root.left, rsum - root.val) or self.hasPathSum(root.right, rsum... | path-sum | (Python) A very simple recursive solution | Aishnupur | 1 | 99 | path sum | 112 | 0.477 | Easy | 857 |
https://leetcode.com/problems/path-sum/discuss/484125/Python-3-(beats-~99)-(nine-lines)-(DFS) | class Solution:
def hasPathSum(self, R: TreeNode, S: int) -> bool:
P, B = [], [0]
def dfs(N):
if N == None or B[0]: return
P.append(N.val)
if (N.left,N.right) == (None,None) and sum(P) == S: B[0] = 1
else: dfs(N.left), dfs(N.right)
P.pop()
... | path-sum | Python 3 (beats ~99%) (nine lines) (DFS) | junaidmansuri | 1 | 525 | path sum | 112 | 0.477 | Easy | 858 |
https://leetcode.com/problems/path-sum/discuss/429483/Python3-recursive-solution-(99.37) | class Solution:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
if not root: return False #non-leaf
if not root.left and not root.right: return root.val == sum #leaf
return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val) | path-sum | Python3 recursive solution (99.37%) | ye15 | 1 | 165 | path sum | 112 | 0.477 | Easy | 859 |
https://leetcode.com/problems/path-sum/discuss/429483/Python3-recursive-solution-(99.37) | class Solution:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
if not root: return sum == 0
return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val) | path-sum | Python3 recursive solution (99.37%) | ye15 | 1 | 165 | path sum | 112 | 0.477 | Easy | 860 |
https://leetcode.com/problems/path-sum/discuss/429483/Python3-recursive-solution-(99.37) | class Solution:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
def fn(node, x):
if not node: return False #non-leaf
if not node.left and not node.right: return node.val == x #leaf node
return fn(node.left, x-node.val) or fn(node.right, x-node.val)
... | path-sum | Python3 recursive solution (99.37%) | ye15 | 1 | 165 | path sum | 112 | 0.477 | Easy | 861 |
https://leetcode.com/problems/path-sum/discuss/429483/Python3-recursive-solution-(99.37) | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
stack = [(root, 0)]
while stack:
node, val = stack.pop()
if node:
val += node.val
if not node.left and not node.right and val == targetSum: return True
... | path-sum | Python3 recursive solution (99.37%) | ye15 | 1 | 165 | path sum | 112 | 0.477 | Easy | 862 |
https://leetcode.com/problems/path-sum/discuss/366622/Python-recursive-and-iterative-using-pre-order | class Solution:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
if not root:
return False
sum -= root.val
if sum == 0 and not root.left and not root.right:
return True
return self.hasPathSum(root.left, sum) or self.hasPathSum(root.right, sum) | path-sum | Python recursive and iterative using pre-order | amchoukir | 1 | 234 | path sum | 112 | 0.477 | Easy | 863 |
https://leetcode.com/problems/path-sum/discuss/366622/Python-recursive-and-iterative-using-pre-order | class Solution:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
if not root:
return False
stack = [(root, sum)]
while stack:
node, sum = stack.pop()
sum -= node.val
if sum == 0 and not node.left and not node.right:
retur... | path-sum | Python recursive and iterative using pre-order | amchoukir | 1 | 234 | path sum | 112 | 0.477 | Easy | 864 |
https://leetcode.com/problems/path-sum/discuss/2827790/simple-python-oror-dfs | class Solution:
def hasPathSum(self, root: Optional[TreeNode], target_sum: int) -> bool:
# if root doesn't exist
if not root:
return False
# update target_sum
target_sum -= root.val
# if leaf node and target_sum reached
if (not root.left)... | path-sum | simple python || dfs | wduf | 0 | 9 | path sum | 112 | 0.477 | Easy | 865 |
https://leetcode.com/problems/path-sum/discuss/2802803/Python-OneLiner | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
return False if not root else (root.val == targetSum and not root.left and not root.right) or self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root.val) | path-sum | Python OneLiner | ryankert | 0 | 2 | path sum | 112 | 0.477 | Easy | 866 |
https://leetcode.com/problems/path-sum/discuss/2660665/Python3!-As-short-as-it-gets! | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if root == None:
return False
if root.left is None and root.right is None:
return targetSum == root.val
return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(r... | path-sum | 😎Python3! As short as it gets! | aminjun | 0 | 28 | path sum | 112 | 0.477 | Easy | 867 |
https://leetcode.com/problems/path-sum/discuss/2660572/Python-Solution | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
def traversal(node, currentSum):
if not node: return False
c = currentSum + node.val
if node.left and node.right:
return traversal(node.left, c) or traversal(node.righ... | path-sum | Python Solution | AxelDovskog | 0 | 5 | path sum | 112 | 0.477 | Easy | 868 |
https://leetcode.com/problems/path-sum/discuss/2659969/94-Faster-than-Solution-or-Easy-to-Understand-or-Python | class Solution(object):
def dfs(self, root, sum_, target):
if not root: return False
if root.left is None and root.right is None and (sum_ + root.val) == target: return True
return self.dfs(root.left, sum_ + root.val, target) or self.dfs(root.right, sum_ + root.val, target)
... | path-sum | 94% Faster than Solution | Easy to Understand | Python | its_krish_here | 0 | 66 | path sum | 112 | 0.477 | Easy | 869 |
https://leetcode.com/problems/path-sum/discuss/2659678/DFS | class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
f = False
def util(root, c):
nonlocal f
if root:
c += root.val
util(root.left, c)
util(root.right, c)
if not root.left and n... | path-sum | DFS | jaisalShah | 0 | 3 | path sum | 112 | 0.477 | Easy | 870 |
https://leetcode.com/problems/path-sum-ii/discuss/484120/Python-3-(beats-~100)-(nine-lines)-(DFS) | class Solution:
def pathSum(self, R: TreeNode, S: int) -> List[List[int]]:
A, P = [], []
def dfs(N):
if N == None: return
P.append(N.val)
if (N.left,N.right) == (None,None) and sum(P) == S: A.append(list(P))
else: dfs(N.left), dfs(N.right)
... | path-sum-ii | Python 3 (beats ~100%) (nine lines) (DFS) | junaidmansuri | 9 | 2,200 | path sum ii | 113 | 0.567 | Medium | 871 |
https://leetcode.com/problems/path-sum-ii/discuss/2615820/Clean-Fast-Python3-or-BFS | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if root is None:
return []
q, paths = deque([(root, targetSum, [])]), []
while q:
cur, target, path = q.pop()
if not (cur.left or cur.right) ... | path-sum-ii | Clean, Fast Python3 | BFS | ryangrayson | 8 | 502 | path sum ii | 113 | 0.567 | Medium | 872 |
https://leetcode.com/problems/path-sum-ii/discuss/2615994/python3-or-very-easy-or-explained-or-tree | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
self.targetSum, self.ans = targetSum, [] # variable initialization
self.get_path_sum(root, 0, []) # calling function get path sum
return self.ans ... | path-sum-ii | python3 | very easy | explained | tree | H-R-S | 2 | 145 | path sum ii | 113 | 0.567 | Medium | 873 |
https://leetcode.com/problems/path-sum-ii/discuss/2362977/Easy-dfs-python-solution | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
# Simple dfs solution
# we have to check both left call, and right call and
# insert path into the final result i.e. res
def helper(root,targetsum,path,res,csum):
if r... | path-sum-ii | Easy dfs python solution | Aniket_liar07 | 2 | 92 | path sum ii | 113 | 0.567 | Medium | 874 |
https://leetcode.com/problems/path-sum-ii/discuss/2320926/Python3-standard-DFS-solution | class Solution:
def pathSum(self, root: Optional[TreeNode], target: int) -> List[List[int]]:
result = []
def dfs(root, target, path):
if not root:
return
if not root.left and not root.right and target - root.val == 0:
result.append(... | path-sum-ii | 📌 Python3 standard DFS solution | Dark_wolf_jss | 2 | 37 | path sum ii | 113 | 0.567 | Medium | 875 |
https://leetcode.com/problems/path-sum-ii/discuss/1079961/Elegant-Python-DFS-solution-faster-than-99.90 | class Solution:
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
def dfs(node, total = 0, path = []):
if not node: return
path.append(node.val)
total += node.val
if not (node.left or node.right) and total == targetSum:
path... | path-sum-ii | Elegant Python DFS solution, faster than 99.90% | 111989 | 2 | 302 | path sum ii | 113 | 0.567 | Medium | 876 |
https://leetcode.com/problems/path-sum-ii/discuss/2441342/Python3-breath-first-search-solution-easy-to-understand | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if root is None: # Corner case
return []
# Initialize variables
result = []
deq = deque()
# Start traversal
# Every element of deque is a s... | path-sum-ii | Python3, breath first search solution, easy to understand | NonameDeadinside | 1 | 30 | path sum ii | 113 | 0.567 | Medium | 877 |
https://leetcode.com/problems/path-sum-ii/discuss/1383147/Python3-dfs | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
ans = []
if root:
mp = {root: None}
stack = [(root, 0)]
while stack:
node, val = stack.pop()
val += node.val
if node.... | path-sum-ii | [Python3] dfs | ye15 | 1 | 38 | path sum ii | 113 | 0.567 | Medium | 878 |
https://leetcode.com/problems/path-sum-ii/discuss/1383097/Python-10-lines-DFS | class Solution:
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
self.ans = []
def dfs(node, path):
if not node: return
if not node.left and not node.right:
if sum(path + [node.val]) == targetSum: self.ans.append(path... | path-sum-ii | Python 10 lines DFS | SmittyWerbenjagermanjensen | 1 | 155 | path sum ii | 113 | 0.567 | Medium | 879 |
https://leetcode.com/problems/path-sum-ii/discuss/1369835/DFS-using-generator-with-5-lines-in-Python-3 | class Solution:
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
def dfs(n: TreeNode, s: int):
if (None, None) == (n.left, n.right) and s == targetSum:
yield [n.val]
else:
yield from ([n.val] + path for c in (n.left, n.right) if c... | path-sum-ii | DFS using generator with 5 lines in Python 3 | mousun224 | 1 | 38 | path sum ii | 113 | 0.567 | Medium | 880 |
https://leetcode.com/problems/path-sum-ii/discuss/571907/Elegant-solution-using-a-single-function-Python-DFS | class Solution:
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
if not root: return []
if not root.left and not root.right:
# leaf node
if root.val == sum:
return [[root.val]]
else:
return []
... | path-sum-ii | Elegant solution using a single function [Python] [DFS] | 1337_baka | 1 | 56 | path sum ii | 113 | 0.567 | Medium | 881 |
https://leetcode.com/problems/path-sum-ii/discuss/2794919/Python3-recursive-solution | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
curr = [] # contains current traversing path
out = [] # contains all found paths
def t(node):
if not node: return # node is null
curr.append(node.val) # append this nod... | path-sum-ii | Python3 recursive solution | lucabonagd | 0 | 3 | path sum ii | 113 | 0.567 | Medium | 882 |
https://leetcode.com/problems/path-sum-ii/discuss/2726523/Python-3-dfs-recursive-sol. | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
res = []
def dfs(v, path, pathsum):
if not v:
return
path.append(v.val)
pathsum += v.val
if not v.left and not v.right and pathsum == target... | path-sum-ii | Python 3 dfs recursive sol. | pranjalmishra334 | 0 | 8 | path sum ii | 113 | 0.567 | Medium | 883 |
https://leetcode.com/problems/path-sum-ii/discuss/2700104/Simple-Beginner-Friendly-Approach-or-DFS-or-O(N)-or-O(N) | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
self.res = []
self.dfs(root, [], targetSum)
return self.res
def dfs(self, root, path, targetSum):
if not root:
return
if not root.left and not root.right:
... | path-sum-ii | Simple Beginner Friendly Approach | DFS | O(N) | O(N) | sonnylaskar | 0 | 11 | path sum ii | 113 | 0.567 | Medium | 884 |
https://leetcode.com/problems/path-sum-ii/discuss/2645451/Python-(Faster-than-98)-or-DFS | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
ans = []
def dfs(node, pathsum, path):
if node:
pathsum += node.val
path.append(node.val)
if not node.left and not node.right:
... | path-sum-ii | Python (Faster than 98%) | DFS | KevinJM17 | 0 | 4 | path sum ii | 113 | 0.567 | Medium | 885 |
https://leetcode.com/problems/path-sum-ii/discuss/2635875/Easy-Python3-solution-using-recursion | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
paths = []
def traverse(path, node):
if(not node):
return
if(node.left == None and node.right == None):
if(sum(path) + node.val != targetSum):
... | path-sum-ii | Easy Python3 solution using recursion | PranavBharadwaj1305 | 0 | 8 | path sum ii | 113 | 0.567 | Medium | 886 |
https://leetcode.com/problems/path-sum-ii/discuss/2618669/Clean-8-line-DFS-in-Python-3 | class Solution:
def pathSum(self, root: Optional[TreeNode], target: int) -> List[List[int]]:
def dfs(n: TreeNode = root, s=0):
s += n.val
if (None, None) == (n.left, n.right) and s == target:
yield [n.val]
else:
for c in filter(Non... | path-sum-ii | Clean 8-line DFS in Python 3 | mousun224 | 0 | 18 | path sum ii | 113 | 0.567 | Medium | 887 |
https://leetcode.com/problems/path-sum-ii/discuss/2618375/Easy-to-read | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
ans = []
def dfs(node, path, ans):
path.append(node.val)
if node.left == None and node.right == None:
if sum(path) == targetSum:
ans.append(path... | path-sum-ii | Easy to read | TheFlyingTurtle46 | 0 | 13 | path sum ii | 113 | 0.567 | Medium | 888 |
https://leetcode.com/problems/path-sum-ii/discuss/2618071/90-Faster-oror-Simple-Python-Solution | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if root is None:
return []
if root.left is None and root.right is None:
if targetSum-root.val==0:
return [[root.val]]
else:
... | path-sum-ii | 90% Faster 🔥 || Simple Python Solution | wilspi | 0 | 8 | path sum ii | 113 | 0.567 | Medium | 889 |
https://leetcode.com/problems/path-sum-ii/discuss/2617988/python3-short-and-simple | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root: return []
self.ans = []
def dfs(node, path, t):
if t == 0 and not node.left and not node.right: self.ans.append(path)
if node.left: dfs(node.left, path+[node.l... | path-sum-ii | python3, short and simple | pjy953 | 0 | 3 | path sum ii | 113 | 0.567 | Medium | 890 |
https://leetcode.com/problems/path-sum-ii/discuss/2617260/Python-Solution-DFS | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
def dfs(node, node_path=[]):
if not node: return
node_path.append(node.val) # append value to the path
if (not node.left) and (not node.righ... | path-sum-ii | Python Solution DFS | remenraj | 0 | 20 | path sum ii | 113 | 0.567 | Medium | 891 |
https://leetcode.com/problems/path-sum-ii/discuss/2617175/Python-Faster-than-98 | class Solution:
paths = []
def traversal(self, node, currentPath):
c = currentPath.copy()
c.append(node.val)
if not node.left and not node.right:
self.paths.append(c)
else:
if node.left:
self.traversal(node.left, c)
if node.rig... | path-sum-ii | Python Faster than 98% | AxelDovskog | 0 | 9 | path sum ii | 113 | 0.567 | Medium | 892 |
https://leetcode.com/problems/path-sum-ii/discuss/2616767/python-easy-peasy-solution-using-dfs-iteration | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root:
return []
res=[]
stk=[[root,root.val,[root.val]]]
while stk:
temp=stk.pop()
if not temp[0].left and not temp[0].right and temp[1]==targetSu... | path-sum-ii | python easy-peasy solution using dfs iteration | benon | 0 | 20 | path sum ii | 113 | 0.567 | Medium | 893 |
https://leetcode.com/problems/path-sum-ii/discuss/2616642/Python-Simple-Python-Solution-Using-DFS-(Recursion)-and-Backtracking | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
self.result_path = []
def DFS(node,target_Sum, current_path):
if node == None:
return []
current_path.append(node.val)
if node.left is None and node.right is None and sum(current_path) == target_Sum:
... | path-sum-ii | [ Python ] ✅✅ Simple Python Solution Using DFS (Recursion) and Backtracking 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 8 | path sum ii | 113 | 0.567 | Medium | 894 |
https://leetcode.com/problems/path-sum-ii/discuss/2616173/Python-Solution-or-Recursion-or-Easy | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
ans=[]
def helper(root, path):
if root is None:
return []
if root.left is None and root.right is None:
if sum(path)+root.val==targetSum:
... | path-sum-ii | Python Solution | Recursion | Easy | Siddharth_singh | 0 | 22 | path sum ii | 113 | 0.567 | Medium | 895 |
https://leetcode.com/problems/path-sum-ii/discuss/2615839/Python-or-DFS | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
self.answer = []
def traverse(root, targetSum, path, currSum):
if not root:
return
path.append(root.val)
currSum += root.val
if not root.le... | path-sum-ii | Python | DFS | everydayspecial | 0 | 17 | path sum ii | 113 | 0.567 | Medium | 896 |
https://leetcode.com/problems/path-sum-ii/discuss/2532847/93ms-or-Python-solution | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
ans = []
def dfs(node: Optional[TreeNode], arr: List[int], resSum: int):
if node == None:
return
arr.append(node.val);
resSum -= node.val;
... | path-sum-ii | 93ms | Python solution | kitanoyoru_ | 0 | 33 | path sum ii | 113 | 0.567 | Medium | 897 |
https://leetcode.com/problems/path-sum-ii/discuss/2488263/Python-solution-using-DFS-TC-%3A-O(N)-SC%3A-O(N) | class Solution:
def solve(self, root, S , curr , res):
if(not root):
return
curr.append(root.val)
if(root.val == S and not root.left and not root.right):
res.append(list(curr))
#remeber that when append lisi of list convert into a list
... | path-sum-ii | Python solution using DFS TC : O(N) SC: O(N) | rajitkumarchauhan99 | 0 | 38 | path sum ii | 113 | 0.567 | Medium | 898 |
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/2340445/Python-or-intuitive-explained-or-O(1)-space-ignoring-recursion-stack-or-O(n)-time | class Solution:
def __init__(self):
self.prev = None
def flatten(self, root: Optional[TreeNode]) -> None:
if not root: return
self.flatten(root.right)
self.flatten(root.left)
root.right = self.prev
root.left = None
self.prev = root | flatten-binary-tree-to-linked-list | Python | intuitive, explained | O(1) space ignoring recursion stack | O(n) time | mync | 7 | 223 | flatten binary tree to linked list | 114 | 0.612 | Medium | 899 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.