count = total minus = 0 while count and not que.empty(): node = que.get() level.append(node.val) if node.left: que.put(node.left) else: minus += 1 if node.right: que.put(node.right) else: minus += 1 count -= 1 ans.append(level[:]) level.clear() total = total * 2 - minus return ans if __name__ == '__main__': tree = '[3,9,20,null,null,15,7]' root = deserialize(tree) drawtree(root) ans = Solution().levelOrder(root) print(ans)
rightRes = self.inOrder(root.right) # 不能用如下表达式 # res = leftRes or rightRes res = leftRes if leftRes != None else rightRes return res def createListByLevel(arr): end = len(arr) index = 0 nodes = [TreeNode(val) if val != None else None for val in arr] while index * 2 + 1 < end: if nodes[index] != None: nodes[index].left = nodes[index * 2 + 1] if index * 2 + 2 < end: nodes[index].right = nodes[index * 2 + 2] index += 1 return nodes[0] if __name__ == '__main__': #arr = [5, 3, 6, 2, 4, None, None, 1] #arr = [31,30,48,3,None,38,49,0,16,35,47,None,None,None,2,15,27,33,37,39,None,1,None,5,None,22,28,32,34,36,None,None,43,None,None,4,11,19,23,None,29,None,None,None,None,None,None,40,46,None,None,7,14,17,21,None,26,None,None,None,41,44,None,6,10,13,None,None,18,20,None,25,None,None,42,None,45,None,None,8,None,12,None,None,None,None,None,24,None,None,None,None,None,None,9] # 这个测试用例主要是0的判断, 所以return时不能用 a or b表达式 root = deserialize( '[31,30,48,3,null,38,49,0,16,35,47,null,null,null,2,15,27,33,37,39,null,1,null,5,null,22,28,32,34,36,null,null,43,null,null,4,11,19,23,null,29,null,null,null,null,null,null,40,46,null,null,7,14,17,21,null,26,null,null,null,41,44,null,6,10,13,null,null,18,20,null,25,null,null,42,null,45,null,null,8,null,12,null,null,null,null,null,24,null,null,null,null,null,null,9]' ) ans = Solution().kthSmallest(root, 1) print(ans)
def lowestCommonAncestor(self, root, p, q): """ :type root: TreeNode :type p: TreeNode :type q: TreeNode :rtype: TreeNode """ if root is None or root.val == p.val or root.val == q.val: return root left = self.lowestCommonAncestor(root.left, p, q) right = self.lowestCommonAncestor(root.right, p, q) if left is not None and right is not None: return root # return the node that is not none or None return left if left is not None else right if __name__ == '__main__': tree = deserialize('[1,2,3,4,5,6,7]') res = Solution().lowestCommonAncestor(tree, TreeNode(4), TreeNode(5)) print(res) tree = deserialize('[1,2,null,3,null,4,null,5]') res = Solution().lowestCommonAncestor(tree, TreeNode(4), TreeNode(5)) print(res)
def isValidBST(self, root): self.prev = None return self.doValidBST(root) def doValidBST(self, root): """ :type root: TreeNode :rtype: bool """ if root is None: return True if not self.doValidBST(root.left): return False if self.prev is not None and root.val <= self.prev.val: return False self.prev = root return self.doValidBST(root.right) if __name__ == '__main__': root = deserialize('[6,3,8,1,4,7,9]') drawtree(root) res = Solution().isValidBST(root) print(res)
# self.val = x # self.left = None # self.right = None # self.next = None from tools.leetcode import deserialize class Solution: # @param root, a tree link node # @return nothing def connect(self, root): if root is None: return None if root.left and root.right: root.left.next = root.right connLeft = root.right if root.right is not None else root.left if connLeft and root.next: connRight = root.next.left if root.next.left else root.next.right connLeft.next = connRight self.connect(root.left) self.connect(root.right) if __name__ == '__main__': tree = deserialize('[1,2,3,4,5,6,7]') Solution().connect(tree) print(tree)
class Solution: def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if root is None: return True return self.doSymmetricRecursive(root.left, root.right) def doSymmetricRecursive(self, nodeLeft, nodeRight): if nodeLeft is None and nodeRight is None: return True if nodeLeft is None or nodeRight is None: return False if nodeLeft.val == nodeRight.val: outPair = self.doSymmetricRecursive(nodeLeft.left, nodeRight.right) inPair = self.doSymmetricRecursive(nodeLeft.right, nodeRight.left) return inPair and outPair return False if __name__ == '__main__': nodes = '[1,2,2,3,4,4,3]' nodes = '[1,2,2,null,3,null,3]' root = deserialize(nodes) res = Solution().isSymmetric(root) print(res)
class Solution: def TwoNodesFather(self, head, n1, n2): if head is None or head.val == n1 or head.val == n2: return head left = self.TwoNodesFather(head.left, n1, n2) right = self.TwoNodesFather(head.right, n1, n2) if left is not None and right is not None: return head return left if left is not None else right if __name__ == '__main__': tree = deserialize('[1, 2, 3, 4, 5, 6, 7]') drawtree(tree) print(Solution().TwoNodesFather(tree, 4, 5)) tree = deserialize('[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]') #drawtree(tree) print(Solution().TwoNodesFather(tree, 4, 5)) tree = deserialize('[1,2,null,3,null,4]') drawtree(tree) print(Solution().TwoNodesFather(tree, 4, 3))