# print(count) return count[k - 1] def helper(self, node, count): if not node: return self.helper(node.left, count) count.append(node.val) self.helper(node.right, count) root = [3, 1, 4, None, 2] k = 1 # Output: 1 # Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively. print('Output :', Solution().kthSmallest(build_tree(root), k)) root = [5, 3, 6, 2, 4, None, None, 1] k = 3 # Output: 3 # Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively. print('Output :', Solution().kthSmallest(build_tree(root), k)) root = [1] k = 1 # Output: 1 # Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively. print('Output :', Solution().kthSmallest(build_tree(root), k)) root = [1, None, 2] k = 2
return False if not root.left and not root.right and root.val == sum: return True sum -= root.val return self.hasPathSum(root.left, sum) or self.hasPathSum( root.right, sum) root = [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1] targetSum = 22 # Output: true print('Output :', Solution().hasPathSum(build_tree(root), targetSum)) root = [1, 2, 3] targetSum = 5 # Output: false print('Output :', Solution().hasPathSum(build_tree(root), targetSum)) root = [] targetSum = 1 # Output: false print('Output :', Solution().hasPathSum(build_tree(root), targetSum)) root = [1, 2] targetSum = 1 print('Output :', Solution().hasPathSum(build_tree(root), targetSum))
import sys sys.path.append('C:\\Users\\rockyo\\Desktop\\leetcode') from ds import build_tree, TreeNode class Solution: def sumOfLeftLeaves(self, root) -> int: if not root: return 0 if not root.left == None: if root.left.left == None and root.left.right == None: return root.left.val + self.sumOfLeftLeaves(root.right) return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves( root.right) root = [3, 9, 20, None, None, 15, 7] # Output: 24 # Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively. print('Output :', Solution().sumOfLeftLeaves(build_tree(root))) root = [1] # Output: 0 print('Output :', Solution().sumOfLeftLeaves(build_tree(root))) root = [1, 2, 3, 4, 5] # Output: 4 print('Output :', Solution().sumOfLeftLeaves(build_tree(root)))
q = queue.Queue() q.put(tree) while (True): if q.empty(): break n = q.get() if n: print(n.val) else: print(None) if not n == None: q.put(n.left) q.put(n.right) root = [3, 9, 20, None, None, 15, 7] Output: 3 print('output:', Solution().maxDepth(build_tree(root))) # tree = build_tree(root) # traverse(tree) root = [1, None, 2] Output: 2 print('output:', Solution().maxDepth(build_tree(root))) root = [] # Output: 0 print('output:', Solution().maxDepth(build_tree(root))) root = [0] Output: 1 print('output:', Solution().maxDepth(build_tree(root)))
ans += [curr.val] if curr.right: s += [curr.right] curr.right = None if curr.left: s += [curr.left] curr.left = None return ans root = [3, 1, 2] Output = [1, 3, 2] print(Solution().postorderTraversal(build_tree(root))) root = [] Output = [] print(Solution().postorderTraversal(build_tree(root))) root = [1] Output = [1] print(Solution().postorderTraversal(build_tree(root))) root = [1, 2] Output = [2, 1] print(Solution().postorderTraversal(build_tree(root))) root = [1, None, 2] Output = [1, 2]