def main(): """Main function""" # Test data test_data = [ [[3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 5, 1, 3], [[3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 6, 4, 5], [[3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 5, 4, 5], [[3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 5, 9, None], ] sol = Solution() for vals, p_val, q_val, ans in test_data: root = make_tree(vals) p = find_node(root, p_val) q = find_node(root, q_val) print("# Input: root = {}, p = {}, q = {} (ans = {})".format( vals, p_val, q_val, ans)) lca = sol.lowestCommonAncestor_v1(root, p, q) print(" Output v1 = {})".format(lca.val if lca else "None")) lca = sol.lowestCommonAncestor_v2(root, p, q) print(" Output v2 = {})".format(lca.val if lca else "None")) lca = sol.lowestCommonAncestor_v3(root, p, q) print(" Output v3 = {})".format(lca.val if lca else "None"))
def main(): """Main function""" # Test data test_data = [[1, 2, 5, 3, 4, None, 6]] sol = Solution() for vals in test_data: print("# Input = {}".format(vals)) root = make_tree(vals) sol.flatten_v1(root) print(" Output v1 = {}".format(right_list(root))) root = make_tree(vals) sol.flatten_v2(root) print(" Output v2 = {}".format(right_list(root)))
def main(): test_data = [ [[1, 2, 3], 6], [[-10, 9, 20, None, None, 15, 7], 42], [[2, -1], 2], ] sol = Solution() for vals, ans in test_data: print("# Input = {} (ans={})".format(vals, ans)) root = make_tree(vals) print(" Output v1 = {}".format(sol.maxPathSum(root)))
def main(): test_data = [ [3, 2, 6, 1, 4, 5, 7], [3, 2, 6, None, None, 5, 7], ] sol = Solution() for vals in test_data: root = make_tree(vals) print("\n# Input: {}".format(vals)) print("=> Output v1 = {}".format(sol.verticalOrder(root)))
def main(): """Main function""" # Test data test_data = [ [4, 2, 7, 1, 3, 6, 9], ] sol = Solution() for vals in test_data: print("# Input: root = {}".format(vals)) root = make_tree(vals) output_v1 = sol.invertTree_v1(root) print(" Output v1 = {} (orig = {})".format(tree_to_list(output_v1), tree_to_list(root))) root = make_tree(vals) output_v2 = sol.invertTree_v2(root) print(" Output v2 = {} (orig = {})".format(tree_to_list(output_v2), tree_to_list(root)))
def main(): """Main function""" # Test data test_data = [[2, 1, 3], [5, 1, 4, None, None, 3, 6], [1, 1]] sol = Solution() for vals in test_data: print("# Input = {}".format(vals)) root = make_tree(vals) print(" Output v1 = {}".format(sol.isValidBST_v1(root))) print(" Output v2 = {}".format(sol.isValidBST_v2(root))) print(" Output v3 = {}".format(sol.isValidBST_v3(root)))
def main(): """Main function""" # Test data test_data = [ [1, 2, 3, None, 5, None, 4], [1, 2, 3, None, 5], ] sol = Solution() for vals in test_data: print("# Input = {}".format(vals)) root = make_tree(vals) print(" View v1 = {}".format(sol.rightSideView_v1(root)))
def main(): """Main function""" # Test data test_data = [ [1, 2, 3, 4, None, 5, 6], [3, 9, 20, None, None, 15, 7], ] sol = Solution() for vals in test_data: root = make_tree(vals) print("# Input: root = {}".format(vals)) print(" Output v1 = {}".format(sol.levelOrder(root)))
def main(): """Main function""" # Test data test_data = [ [1, 2, 3, None, 5], ] sol = Solution() for vals in test_data: root = make_tree(vals) print("# Input: {}".format(vals)) print(" Output v1 = {}".format(sol.binaryTreePaths_v1(root))) print(" Output v2 = {}".format(sol.binaryTreePaths_v2(root)))
def main(): """Main function""" # Test data test_data = [ [1, 2, 2, 3, 4, 4, 3], [1, 2, 2, None, 3, None, 3], ] sol = Solution() for vals in test_data: root = make_tree(vals) print("# Input: root = {}".format(vals)) print(" Output v1 = {}".format(sol.isSymmetric_v1(root))) print(" Output v2 = {}".format(sol.isSymmetric_v2(root)))
def main(): """Main function""" # Test data test_data = [ [1, 2, 3, 4, 5], [6, 1, None, 2, 3, 4, 5, None, 7], [3, None, 2, 1, 4], ] sol = Solution() for vals in test_data: root = make_tree(vals) print("\n# Input: {}".format(vals)) print("=> Output v1 = {}".format(sol.diameterOfBinaryTree_v1(root)))