def test_3_2(self): tree = TreeNode() tree.build_from_heap([ 1, None, 2, None, None, None, 3, None, None, None, None, None, None, None, 4, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 5 ]) self.assertEqual(path_sum(tree, 3), 2)
def test_3_1(self): tree = TreeNode() tree.build_from_heap([3]) self.assertEqual(max_depth(tree), 1)
def test_3920157_3(self): tree = TreeNode() tree.build_from_heap([3, 9, 20, None, None, 15, 7]) self.assertEqual(max_depth(tree), 3)
def test_two_false(self): tree1 = TreeNode() tree2 = TreeNode() tree1.build_from_heap([1]) tree2.build_from_heap([2]) self.assertFalse(same_tree(tree1, tree2))
def test_two_paths_none(self): tree = TreeNode() tree.build_from_heap([6, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, None, 5, 1]) self.assertEqual(path_sum(tree, 22), [])
def test_5_digits(self): built_tree = convert_array([-10, -3, 0, 5, 9]) test_tree = TreeNode() test_tree.build_from_heap([0, -3, 9, -10, None, 5]) self.assertEqual(built_tree, test_tree)
def test_1_false(self): tree = TreeNode() tree.build_from_heap([1]) self.assertFalse(path_sum(tree, 20))
def test_1000_false(self): tree = TreeNode() tree.build_from_heap( [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, None, None, 1]) self.assertFalse(path_sum(tree, 1000))
def test_single(self): input_tree = TreeNode() input_tree.build_from_heap([1]) test_tree = TreeNode() test_tree.build_from_heap([1]) self.assertEqual(invert_bin_tree(input_tree), test_tree)
def test_4271369(self): input_tree = TreeNode() input_tree.build_from_heap([4, 2, 7, 1, 3, 6, 9]) test_tree = TreeNode() test_tree.build_from_heap([4, 7, 2, 9, 6, 3, 1]) self.assertEqual(invert_bin_tree(input_tree), test_tree)
from pathlib import Path import sys sys.path.append(str(Path(__file__).parent.parent.absolute())) from models.binary_tree import TreeNode, arr_to_node class Solution: def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: while p != None or q != None: if p == None or q == None or (p.val != q.val): return False else: if not self.isSameTree(p.left, q.left): return False if not self.isSameTree(p.right, q.right): return False return True return True s = Solution() p = TreeNode(1) p.left = TreeNode(2) p.right = TreeNode(3) q = TreeNode(1) q.left = TreeNode(2) result = s.isSameTree(p, q) print(result)
def test_short_5(self): tree = TreeNode() tree.build_from_heap([2, 3, 3]) self.assertEqual(path_sum(tree, 5), [[2, 3], [2, 3]])
def test_one_path_26(self): tree = TreeNode() tree.build_from_heap([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, None, 5, 1]) self.assertEqual(path_sum(tree, 26), [[5, 8, 13]])
def test_long_4(self): tree = TreeNode() tree.build_from_heap([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) self.assertEqual(max_depth(tree), 4)
def test_small_2(self): input_tree = TreeNode() input_tree.build_from_heap([1, 2]) test_tree = TreeNode() test_tree.build_from_heap([1, None, 2]) self.assertEqual(invert_bin_tree(input_tree), test_tree)
def test_26_true(self): tree = TreeNode() tree.build_from_heap( [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, None, None, 1]) self.assertTrue(path_sum(tree, 26))
def test_small_asym_false(self): tree1 = TreeNode() tree2 = TreeNode() tree1.build_from_heap([1, 2]) tree2.build_from_heap([1, None, 2]) self.assertFalse(same_tree(tree1, tree2))
def test_1_true(self): tree = TreeNode() tree.build_from_heap([1]) self.assertTrue(path_sum(tree, 1))
def test_blank_false(self): tree1 = TreeNode() tree2 = TreeNode() tree1.build_from_heap([1, 2, 3, 4]) tree2.build_from_heap([]) self.assertFalse(same_tree(tree1, tree2))
def test_blank_false(self): tree = TreeNode() tree.build_from_heap([]) self.assertFalse(path_sum(tree, 14))
def test_one_true(self): tree1 = TreeNode() tree2 = TreeNode() tree1.build_from_heap([1]) tree2.build_from_heap([1]) self.assertTrue(same_tree(tree1, tree2))
# @Date : 2019/5/21 14:59 # @Author: hlliu from queue import LifoQueue from models.binary_tree import TreeNode class Solution: def preorderTraversal(self, root: TreeNode): res = [] stack = LifoQueue() cur = root while cur is not None or not stack.empty(): if cur is not None: stack.put(cur) res.append(cur.val) cur = cur.left else: cur = stack.get().right return res root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5) s = Solution() print(s.preorderTraversal(root))
def test_two_paths_22(self): tree = TreeNode() tree.build_from_heap([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, None, 5, 1]) self.assertEqual(path_sum(tree, 22), [[5, 4, 11, 2], [5, 8, 4, 5]])