def deserializeHelper(nodesLeft): if (len(nodesLeft) == 0): return value = nodesLeft.popleft() if ("X" == value): return None newNode = TreeNode(value) newNode.left = deserializeHelper(nodesLeft) newNode.right = deserializeHelper(nodesLeft) return newNode
def main(): t1 = TreeNode(2) for i in [1, 4, 3, 5]: t1.insert(i) ser = serialize(t1) print(ser) print(deserialize(ser).__str__()) print("================") t2 = TreeNode(2) ser = serialize(t2) print(ser) print(deserialize(ser).__str__())
def buildTree(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(preorder[0]) root.left = buildTree(preorder[1:mid + 1], inorder[:mid]) root.right = buildTree(preorder[mid + 1:], inorder[mid + 1:]) return root
def main(): t1 = TreeNode(4) for i in [2, 7, 1, 3, 6, 9]: t1.insert(i) t1.__str__() invertTree(t1) print("=========") t1.__str__()
def main(): t1 = TreeNode(4) for n in [0, 3, 1, 5, 6, 2, 7]: t1.insert(n) assert(levelOrder(t1) == [[4], [3, 5], [1, 6], [2, 7]]) t2 = TreeNode(1) assert(levelOrder(t2) == [[1]]) t3 = TreeNode(5) for i in [2, 6, 7, 1, 8]: t3.insert(i) assert(levelOrder(t3) == [[5], [2, 6], [1, 7], [8]])
def main(): tree1 = TreeNode(1) for i in [2, 3, 4, 5, 6]: tree1.insert(i) assert (maxDepth(tree1) == 6) tree2 = TreeNode(5) for i in [3, 4, 6, 8, 10, 1]: tree2.insert(i) assert (maxDepth(tree2) == 4) tree3 = TreeNode(10) assert (maxDepth(tree3) == 1)
def main(): t1 = TreeNode(1) for i in [2, 3]: t1.insert(i) assert (maxPathSum(t1) == 6) t2 = TreeNode() for i in [-10, 9, 20, 15, 7]: t2.insert(i) assert (maxPathSum(t2) == 51)
def main(): t1 = TreeNode(6) for i in [4, 9, 3, 1, 10]: t1.insert(i) assert (kthSmallest(t1, 4) == 6) t2 = TreeNode(5) for i in [3, 4, 6, 2, 1]: t2.insert(i) assert (kthSmallest(t2, 1) == 1) assert (kthSmallest(t2, 3) == 3) t3 = TreeNode(10) for i in [12, 15, 20]: t3.insert(i) assert (kthSmallest(t3, 4) == 20)
def main(): t1 = TreeNode(5) for i in [1, 8, 10, 7, 3, 0]: t1.insert(i) assert (isValidBST(t1)) t2 = TreeNode(5) t2.left = TreeNode(8) assert (not isValidBST(t2)) t3 = TreeNode(0) t3.left = TreeNode(-1) t3.right = TreeNode(6) t3.left.left = TreeNode(4) assert (not isValidBST(t3))
def main(): t1 = TreeNode(4) for i in [3, 5, 1]: t1.insert(i) s1 = TreeNode(4) for i in [3, 5]: s1.insert(i) assert (not isSubtree(t1, s1)) t2 = TreeNode(1) s2 = TreeNode(0) assert (not isSubtree(t2, s2)) t3 = TreeNode(7) for i in [3, 6, 1, 10, 8, 12]: t3.insert(i) s3 = TreeNode(10) for i in [8, 12]: s3.insert(i) assert (isSubtree(t3, s3))
def main(): t1 = TreeNode(6) t1Left = TreeNode(3) t1Right = TreeNode(8) t1RR = TreeNode(10) for i in [t1Left, t1Right, t1RR]: t1.insertNode(i) assert (lowestCommonAncestor(t1, t1Right, t1RR) == t1Right) t2 = TreeNode(6) t2Left = TreeNode(2) t2Right = TreeNode(10) t2LR = TreeNode(3) t2LL = TreeNode(1) for i in [t2Left, t2LL, t2LR]: t2.insertNode(i) assert (lowestCommonAncestor(t2, t2LL, t2LR) == t2Left) assert (lowestCommonAncestor(t2, t2Left, t2Right) == t2)
def main(): p1 = TreeNode(4) q1 = TreeNode(4) for i in [2, 9, 10, 6]: q1.insert(i) p1.insert(i) assert(isSameTree(p1, q1)) p2 = TreeNode(4) q2 = TreeNode(4) for i in [2, 11, 7]: q2.insert(i) for i in [0, 5, 6]: p2.insert(i) assert ( not isSameTree(p2, q2)) p3= TreeNode() q3 = TreeNode(4) for i in [1, 2, 3, 5, 6]: q3.insert(i) assert(not isSameTree(p3, q3)) assert(not isSameTree(q3, p3)) p4 = TreeNode() q4 = TreeNode() assert isSameTree(p4, q4)