Runtime beat 100%, 6 lines JAVA recursive solution


  • 1
    L
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
    	if (t1 == null) return t2;
    	if (t2 == null) return t1;
    
    	TreeNode temp = new TreeNode(t1.val + t2.val);
    	temp.left = mergeTrees(t1.left, t2.left);
    	temp.right = mergeTrees(t1.right, t2.right);
    
    	return temp;
    }

  • 0
    S

    how to calculate the time complexity ?


  • 0
    L

    The runtime for this solution is O(n) as we are just traversing each node element in the tree.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.