Created
November 23, 2019 12:58
-
-
Save zack-w/f04d38363e06fee8efbe295d8324035c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { | |
//base case | |
if(t1==null&&t2==null) return null; | |
if(t1==null) return t2; | |
if(t2==null) return t1; | |
//divide and conquer | |
TreeNode root = new TreeNode(t1.val+t2.val); | |
root.left = mergeTrees(t1.left, t2.left); | |
root.right = mergeTrees(t1.right, t2.right); | |
return root; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment