Easy Soln Java - recursion

programming
Tags: #<Tag:0x00007f2422fe55e0>

#1

/**

  • Definition for binary tree
  • class TreeNode {
  • int val;
    
  • TreeNode left;
    
  • TreeNode right;
    
  • TreeNode(int x) {
    
  •  val = x;
    
  •  left=null;
    
  •  right=null;
    
  • }
    
  • }
    */
    public class Solution {
    public TreeNode solve(TreeNode t1, TreeNode t2) {
    if(t1 == null && t2 == null)
    return null;
    else if(t1 == null)
    return t2;
    else if(t2 == null)
    return t1;
    else {
    TreeNode root = new TreeNode(t1.val + t2.val);
    root.left = solve(t1.left,t2.left);
    root.right = solve(t1.right,t2.right);
    return root;
    }
    }
    }