Unable to merge two binary trees using recursion and I want to know mistake which I am committing

  binary-tree, python-3.x, recursion

I was solving question of merging two binary trees in LeetCode using recursion but recursion is not working. But recursion is not working in my code.I want to know where I am making mistake.

class Solution:
    
    def merge(self, root1, root2):
        if root1 != None and root2 != None:
            root1.val += root2.val
            
        if root1 == None and root2 == None:
            return
        
        if root1 != None and root2 == None:
            return
        
        if root1 == None and root2 != None:
            root1= root2
            
        merge(root1.left, root2.left)
        merge(root1.right, root2.right)
        
    def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode:
        if t2 == None:
            return t1
        
        if t2.left == None and t2.right == None:
            t1.val += t2.val
            return t1
        self.merge(t1, t2)
        return t1

This is the question link
https://leetcode.com/problems/merge-two-binary-trees/

Source: Python-3x Questions

LEAVE A COMMENT