data_structures.binary_tree.merge_two_binary_trees ================================================== .. py:module:: data_structures.binary_tree.merge_two_binary_trees .. autoapi-nested-parse:: Problem Description: Given two binary tree, return the merged tree. The rule for merging is that if two nodes overlap, then put the value sum of both nodes to the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. Attributes ---------- .. autoapisummary:: data_structures.binary_tree.merge_two_binary_trees.tree1 Classes ------- .. autoapisummary:: data_structures.binary_tree.merge_two_binary_trees.Node Functions --------- .. autoapisummary:: data_structures.binary_tree.merge_two_binary_trees.merge_two_binary_trees data_structures.binary_tree.merge_two_binary_trees.print_preorder Module Contents --------------- .. py:class:: Node(value: int = 0) A binary node has value variable and pointers to its left and right node. .. py:attribute:: left :type: Node | None :value: None .. py:attribute:: right :type: Node | None :value: None .. py:attribute:: value :value: 0 .. py:function:: merge_two_binary_trees(tree1: Node | None, tree2: Node | None) -> Node | None Returns root node of the merged tree. >>> tree1 = Node(5) >>> tree1.left = Node(6) >>> tree1.right = Node(7) >>> tree1.left.left = Node(2) >>> tree2 = Node(4) >>> tree2.left = Node(5) >>> tree2.right = Node(8) >>> tree2.left.right = Node(1) >>> tree2.right.right = Node(4) >>> merged_tree = merge_two_binary_trees(tree1, tree2) >>> print_preorder(merged_tree) 9 11 2 1 15 4 .. py:function:: print_preorder(root: Node | None) -> None Print pre-order traversal of the tree. >>> root = Node(1) >>> root.left = Node(2) >>> root.right = Node(3) >>> print_preorder(root) 1 2 3 >>> print_preorder(root.right) 3 .. py:data:: tree1