def build(start1, end2, length): if not length: return None root_val = postorder[end2] root = TreeNode(root_val) if length == 1: return root root_index = inorder.index(root_val) left_length = root_index - start1 right_length = length - left_length - 1 root.left = build(start1, end2 - right_length - 1, left_length) root.right = build(start1 + 1 + left_length, end2 - 1, right_length) return root
def build(start1, start2, length): if not length: return None root_val = preorder[start1] root = TreeNode(root_val) if length == 1: return root root_index = inorder.index(root_val) left_length = root_index - start2 right_length = length - left_length - 1 root.left = build(start1 + 1, start2, left_length) root.right = build(start1 + 1 + left_length, start2 + 1 + left_length, right_length) return root
def sortedListToBST(self, head): """ 升序链表,转二叉搜索树 :type head: ListNode :rtype: TreeNode """ if not head: return if not head.next: return TreeNode(head.val) slow, fast = head, head.next.next while fast and fast.next: fast = fast.next.next slow = slow.next tmp = slow.next slow.next = None root = TreeNode(tmp.val) root.left = self.sortedListToBST(head) root.right = self.sortedListToBST(tmp.next) return root
:rtype: bool """ if not root: return False sum -= root.val if not root.left and not root.right and not sum: return True return self.hasPathSum(root.left, sum) or self.hasPathSum( root.right, sum) def f2(self, root, sum): stack = [root] while stack and root: cur = stack.pop() if not cur.left and not cur.right and cur.val == sum: return True if cur.right: cur.right.val += cur.val stack.append(cur.right) if cur.left: cur.left.val += cur.val stack.append(cur.left) return False if __name__ == '__main__': s = Solution() root = TreeNode([5, 4, 8, 11, None, 13, 4, 7, 2, None, 1]) root = TreeNode([1, 2]) print(s.hasPathSum(root, 3))
pro_val = None while p or stack: while p: stack.append(p) p = p.left p = stack.pop() if pro_val and p.val == pro_val.val: cur_count += 1 else: cur_count = 1 pro_val = p if cur_count == max_count: ans.append(p.val) elif cur_count > max_count: ans = [p.val] max_count = cur_count p = p.right return ans if __name__ == '__main__': s = Solution() print(s.findMode(TreeNode([2, 1, 3, 1]))) print(s.findMode(TreeNode([1, None, 2]))) print(s.findMode(TreeNode([2, 1]))) print(s.findMode(TreeNode([1, 2, 3]))) print(s.findMode(TreeNode([2, 2, 3]))) print(s.findMode(TreeNode([1, None, 2, 2])))
res += tmp * 10 + p.val pre = p p = None else: stack.append(p) tmp = tmp * 10 + p.val p = p.left if p.left else p.right # 向下 else: top = stack[-1] if pre is top.left: # 如果上一次是左孩子 if top.right: p = top.right else: p = None pre = stack.pop() tmp //= 10 else: pre = stack.pop() tmp //= 10 return res if __name__ == '__main__': s = Solution() print(s.sumNumbers(TreeNode([1, 2, 3, 4, 5, 6, 7]))) print(s.sumNumbers(TreeNode([1, 2, 3]))) print(s.sumNumbers(TreeNode([1]))) # root = TreeNode([1, 2, 3, 4]) print(s.sumNumbers(TreeNode([1, 2, 3, 4]))) print(s.sumNumbers(TreeNode([1, 2, 3, None, 4, None, 5])))
from base.Tree import TreeNode class Solution(object): def findBottomLeftValue(self, root): """ :type root: TreeNode :rtype: int """ from collections import deque q1 = deque() q2 = deque() q1.append(root) while q1: ans = q1[0] while q1: node = q1.popleft() if node.left: q2.append(node.left) if node.right: q2.append(node.right) q1 = q2 q2 = deque() return ans.val if __name__ == '__main__': s = Solution() print(s.findBottomLeftValue(TreeNode([3, 1, 5, 0, 2, 4, 6])))
""" :type root: TreeNode :type sum: int :rtype: List[List[int]] """ res = [] if not root: return res sum -= root.val if not root.left and not root.right: if sum == 0: return [[root.val]] else: return [] if root.left: res += self.pathSum(root.left, sum) if root.right: res += self.pathSum(root.right, sum) return [[root.val] + i for i in res] if __name__ == '__main__': s = Solution() root = TreeNode([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1]) # print(root.left.left.left.left) # root = TreeNode(22, 0, 0) print(s.pathSum(root, 22)) # print(root)
class Solution(object): def findFrequentTreeSum(self, root): """ :type root: TreeNode :rtype: List[int] """ from collections import defaultdict d = defaultdict(int) self.max_time = 0 def foo(tree: TreeNode): if not tree: return 0 l = foo(tree.left) r = foo(tree.right) val = tree.val + l + r d[val] += 1 if d[val] > self.max_time: self.max_time = d[val] return val foo(root) return [i for i, j in d.items() if j == self.max_time] if __name__ == '__main__': s = Solution() print(s.findFrequentTreeSum(TreeNode([5, 2, -3])))
""" 把二叉树按照先序摊平,每个节点边做其先序前一节点的右孩子 :type root: TreeNode :rtype: void Do not return anything, modify root in-place instead. """ def find_last(root): tmp = root while tmp.right or tmp.left: if tmp.right: tmp = tmp.right elif tmp.left: tmp = tmp.left return tmp tmp = root while tmp: # print(tmp.val) if tmp.left: left_last = find_last(tmp.left) left_last.right = tmp.right tmp.right = tmp.left tmp.left = None tmp = tmp.right if __name__ == '__main__': s = Solution() root = TreeNode([1, 2, 3, 4, 5, 6, 7]) s.flatten(root) print(root)
""" if not root: return [] from collections import deque MIN = -0xffffffff q1 = deque() q2 = deque() q1.append(root) ans = [] while q1: max_val = MIN while q1: node = q1.popleft() if node.val > max_val: max_val = node.val if node.left: q2.append(node.left) if node.right: q2.append(node.right) q1 = q2 q2 = deque() ans.append(max_val) return ans if __name__ == '__main__': s = Solution() print(s.largestValues(TreeNode([1, 3, 2, 5, 3, None, 9]))) print(s.largestValues(TreeNode([1]))) print(s.largestValues(TreeNode([])))
def isValidBST(self, root): """ 判断一个树是否为二叉搜索树 :type root: TreeNode :rtype: bool """ if root is None: return True p = root stack = [] per = float('-inf') while len(stack) > 0 or p is not None: while p is not None: stack.append(p) p = p.left if len(stack) > 0: p = stack.pop() if p.val <= per: return False per = p.val p = p.right return True if __name__ == '__main__': s = Solution() print(s.isValidBST(TreeNode([2, 1, 4, null, null, 3, 5])))
result.append(current_level) else: current_level.append(node.val) if node.left: if flag: first = node.left flag = False lst.append(node.left) if node.right: if flag: first = node.right flag = False lst.append(node.right) return result def soluion3(self, root): ans, level = [], [root] while root and level: ans.append([node.val for node in level]) level = [kid for n in level for kid in (n.left, n.right) if kid] return ans if __name__ == '__main__': null = None root = TreeNode([3, 9, 20, null, null, 15, 7]) # root = TreeNode([]) s = Solution() print(s.soluion3(root))