# How to use merge_trees method in autotest

Best Python code snippet using autotest_python

MergeTwoBinaryTrees.py

Source:MergeTwoBinaryTrees.py

`...23# 5 4 724#25# Note: The merging process must start from the root nodes of both trees.26from BinaryTrees import TreeNode, binary_tree27def merge_trees(t1, t2):28 if t1 is None:29 return t230 if t2 is None:31 return t132 t = TreeNode(t1.val + t2.val)33 t.left = merge_trees(t1.left, t2.left)34 t.right = merge_trees(t1.right, t2.right)35 return t36# O(k) space37# def merge_trees(t1, t2):38# if t1 is None:39# return t240# if t2 is None:41# return t142# t1.val += t2.val43# t1.left = merge_trees(t1.left, t2.left)44# t1.right = merge_trees(t1.right, t2.right)45# return t146t1 = binary_tree([1, 3, 2, 5])47print(t1)48t2 = binary_tree([2, 1, 3, None, 4, None, 7])49print(t2)...`

test_merge_two_binary_trees.py

Source:test_merge_two_binary_trees.py

`1from typing import List2from core.model.treenode import TreeNode3from core.testing.tree import build_compact_tree, trees_are_equivalent4from src.merge_two_binary_trees.solution import Solution5def merge_trees(l1: List[int], l2: List[int]) -> TreeNode:6 r1 = build_compact_tree(l1)7 r2 = build_compact_tree(l2)8 return Solution().mergeTrees(r1, r2)9class TestMergeTwoBinaryTrees:10 def test_empty_trees(self):11 merged = merge_trees([], [])12 assert merged is None13 def test_one_empty_tree(self):14 l1 = [5, 3, 7, None, 6, 1, None, 8]15 merged = merge_trees(l1, [])16 expected = build_compact_tree(l1)17 assert trees_are_equivalent(merged, expected)18 def test_two_trees_with_same_structure(self):19 merged = merge_trees([5, 3, 7, None, 6, 1, None, 8], [7, 21, 2, None, 4, 10, None, 3])20 expected = build_compact_tree([12, 24, 9, None, 10, 11, None, 11])21 assert trees_are_equivalent(merged, expected)22 def test_example_1(self):23 merged = merge_trees([1, 3, 2, 5], [2, 1, 3, None, 4, None, 7])24 expected = build_compact_tree([3, 4, 5, 5, 4, None, 7])25 assert trees_are_equivalent(merged, expected)26 def test_example_2(self):27 merged = merge_trees([1], [1, 2])28 expected = build_compact_tree([2, 2])...`

merge_two_binary_trees.py

Source:merge_two_binary_trees.py

`1from typing import Optional2from leetcode import TreeNode, test, new_tree3def merge_trees(lhs: Optional[TreeNode], rhs: Optional[TreeNode]) -> Optional[TreeNode]:4 if lhs and rhs:5 lhs.val += rhs.val6 lhs.left = merge_trees(lhs.left, rhs.left)7 lhs.right = merge_trees(lhs.right, rhs.right)8 return lhs9 elif lhs:10 return lhs11 elif rhs:12 return rhs13 else:14 return None15test(16 merge_trees,17 [18 (19 new_tree(1, 3, 2, 5),20 new_tree(2, 1, 3, None, 4, None, 7),21 new_tree(3, 4, 5, 5, 4, None, 7),...`

## Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.