def mergeTrees(t1: TreeNode, t2: TreeNode) -> TreeNode:
    if not t1:
        return t2
    if not t2:
        return t1

    t1.left = mergeTrees(t1.left, t2.left)
    t1.right = mergeTrees(t1.right, t2.right)
    t1.val += t2.val
    return t1
Exemple #2
0
def invertTree(root: TreeNode) -> TreeNode:
    if not root:
        return None

    root.left, root.right = invertTree(root.right), invertTree(root.left)

    return root
 def test_same_single_value(self):
     self.assertTrue(isSameTree(TreeNode(1), TreeNode(1)))
 def test_one_none_other_not(self):
     self.assertFalse(isSameTree(None, TreeNode(1)))
 def test_single_node_no_sum(self):
     self.assertFalse(hasPathSum(TreeNode(1), 22))
 def test_single_node_with_sum(self):
     self.assertTrue(hasPathSum(TreeNode(1), 1))
Exemple #7
0
 def test_empty_in_single_node(self):
     self.assertTrue(isSubtree(TreeNode(1), None))
Exemple #8
0
 def test_single_node(self):
     self.assertEqual(minDepth(TreeNode(1)), 1)
 def test_single_node(self):
     self.assertEqual(sumOfLeftLeaves(TreeNode(4)), 0)
 def test_node_plus_node(self):
     self.assertEqual(mergeTrees(TreeNode(1), TreeNode(2)).val, 3)
 def test_none_plus_node(self):
     self.assertEqual(mergeTrees(None, TreeNode(1)).val, 1)
 def test_node_plus_none(self):
     self.assertEqual(mergeTrees(TreeNode(1), None).val, 1)
 def test_single_node(self):
     self.assertEqual(diameterOfBinaryTree(TreeNode(1)), 0)
Exemple #14
0
 def test_not_found_in_tree(self):
     self.assertFalse(isSubtree(binarytree([1, 4, 5]), TreeNode(3)))
Exemple #15
0
 def test_subtree_in_child(self):
     self.assertTrue(isSubtree(binarytree([1, 2, 3]), TreeNode(2)))
Exemple #16
0
 def test_single_node(self):
     self.assertListEqual(to_list(invertTree(TreeNode(1))), [1])
Exemple #17
0
 def test_single_node(self):
     self.assertListEqual(binaryTreePaths(TreeNode(1)), ["1"])
Exemple #18
0
 def test_root_node_only(self):
     self.assert_list_of_lists_equal(zigzagLevelOrder(TreeNode(1)), [[1]])
Exemple #19
0
 def test_single_node(self):
     self.assertTrue(isBalanced(TreeNode(1)))
 def test_single_node(self):
     self.assertTrue(isSymmetric(TreeNode(1)))
 def test_single_node(self):
     self.assertTrue(isUnivalTree(TreeNode(1)))
Exemple #22
0
 def test_single_node(self):
     self.assertEqual(maxDepth(TreeNode(3)), 1)