Zack Shorts - Interview Prep
GithubLinkedInLeetCode
  • leetcode
    • 0001 - Two Sum
    • 0002 - Add Two Numbers
    • 0003 - Longest Substring Without Repeating Characters
    • 0004 - Median of Two Sorted Arrays
    • 0005 - Longest Palindromic Substring
    • 0006 - ZigZag Conversion
    • 0007 - Reverse Integer
    • 0009 - Palindrome Number
    • 0011 - Container With Most Water
    • 0013 - Roman to Integer
    • 0014 - Longest Common Prefix
    • 0015 - 3 Sum
    • 0017 - Letter Combinations of a Phone Number
    • 0019 - Remove Nth Node From End of List
    • 0020 - Valid Parentheses
    • 0021 - Merge Two Sorted Lists
    • 0022 - Generate Parentheses
    • 0026 - Remove Duplicates from Sorted Array
    • 0027 - Remove Element
    • 0028 - Implement strStr()
    • 0033 - Searching in Rotated Sorted Array
    • 0034 - Find First and Last Position of Element in Sorted Array
    • 0035 - Search Insert Position
    • 0036 - Valid Sudoku
    • 0039 - Combination Sum
    • 0040 - Combination Sum II
    • 0042 - Multiply Strings
    • 0045 - Jump Game II
    • 0046 - Permutations
    • 0047 - Permutations II
    • 0048 - Rotate Image
    • 0049 - Group Anagrams
    • 0053 - Maximum Subarray
    • 0055 - Jump Game
    • 0056 - Merge Intervals
    • 0058 - Length of Last Word
    • 0059 - Spiral Matrix II
    • 0062 - Unique Paths
    • 0066 - Plus One
    • 0067 Add Binary
    • 0069 - Sqrt(x)
    • 0070 - Climbing Stairs
    • 0072 - Edit Distance
    • 0074 - Search a 2D Matrix
    • 0075 - Sort Colors
    • 0076 - Design HashMap
    • 0077 - Combinations
    • 0078 - Subsets
    • 0079 - Word Search
    • 0082 - Remove Duplicates from Sorted List II
    • 0083 - Remove Duplicates from Sorted List
    • 0088 - Merge Sorted Array
    • 0090 - Subsets II
    • 0091 - Decode Ways
    • 0094 - Binary Tree Inorder Traversal
    • 0098 - Validate Binary Search Tree
    • 0100 - Same Tree
    • 0101 - Symmetric Tree
    • 0102 - Binary Tree Level Order Traversal
    • 0104 - Maximum Depth of Binary Tree
    • 0108 - Convert Sorted Arary to Binary Search Tree
    • 0110 - Balance Binary Tree
    • 0111 - Minimum Depth of Binary Tree
    • 0112 - Path Sum
    • 0116 - Populating Next Right Pointers in Each Node
    • 0117 - Populating Next Right Pointers in Each Node 2
    • 0118 - Pascal's Triangle
    • 0119 - Pascal's Triangle II
    • 0120 - Triangle
    • 0121 - Best Time to Buy and Sell Stock
    • 0130 - Surrounded Regions
    • 0136 - Single Number
    • 0139 - Word Break
    • 0141 - Linked List Cycle
    • 0142 - Linked List Cycle 2
    • 0144 - Binary Tree Preorder Traversal
    • 0145 - Binary Tree Postorder Traversal
    • 0146 - LRU Cache
    • 0149 - Max Points on a Line
    • 0152 - Maximum Product Subarray
    • 0153 - Find Minimum in Rotated Sorted Array
    • 0160 - Intersection of Two Linked Lists
    • 0162 - Find Peak Element
    • 0167 - Two Sum II - Input Array Is Sorted
    • 0169 - Majority Element
    • 0187 - Repeated DNA Subsequences
    • 0189 - Rotate Array
    • 0190 - Reverse Bits
    • 0191 - Number of 1 Bits
    • 0198 - House Robber
    • 0200 - Number of Islands
    • 0201 - Bitwise AND of Numbers Range
    • 0202 - Happy Number
    • 0203 - Remove Linked List Elements
    • 0206 - Reverse Linked List
    • 0209 - Minimum Size Subarray Sum
    • 0213 - House Robber II
    • 0217 - Contains Duplicates
    • 0226 - Invert Binary Tree
    • 0231 - Power of Two
    • 0232 - Implement Queue using Stacks
    • 0235 - Lowest Common Ancestor of a Binary Search Tree
    • 0238 - Product of Sorted Array Except Self
    • 0240 - Search a 2D Matrix II
    • 0242 - Valid Anagram
    • 0278 - First Bad Version
    • 0283 - Move Zeroes
    • 0290 - Word Pattern
    • 0300 - Longest Increasing Subsequence
    • 0322 - Coin Change
    • 0334
    • 0343 - Integer Break
    • 0344 - Reverse String
    • 0350 - Intersection of Two Arrays II
    • 0383 - Ransom Note
    • 0384 - Shuffle an Array
    • 0387 - First Unique Character in a String
    • 0409 - Longest Palindrome
    • 0413 - Arithmetic Slices
    • 0415 - Add String
    • 0435 - Non-overlapping Intervals
    • 0438 - Find All Anagrams in a String
    • 0485 - Max Consecutive Ones
    • 0542 - Zero One Matrix
    • 0547 - Number of Provinces
    • 0557 - Reverse Words in a String III
    • 0560 - Subarray Sum Equals K
    • 0556 - Reshape the Matrix
    • 0567 - Permutation in String
    • 572 - Subtree of Another Tree
    • 0583 - Delete Operation for Two Strings
    • 0617 - Merge Two Binary Trees
    • 0653 - Two Sum IV - Input is a BST
    • 0673 - Number of Longest Increasing Subsequence
    • 0695 - Max Area of Island
    • 0700 - Search in a Binary Search Tree
    • 0701 - Insert into a Binary Search Tree
    • 0704 - Binary Search
    • 0713 - Subarray Product Less Than K
    • 0733 - Flood Fill
    • 0763 - Partition Labels
    • 0784 - Letter Case Permutation
    • 797 - All Paths From Source to Target
    • 0844 - Backspace String Compare
    • 0876 - Middle of the Linked List
    • 0937 - Reorder Data in Log Files
    • 0938 - Range Sum of BST
    • 0941 - Valid Mountain Array
    • 0977 - Squares of a Sorted Array
    • 0986 - Interval List Intersections
    • 0542 - 01 Matrix
    • 1041 - Robot Bounded in Circle
    • 1089 - Duplicate Zeros
    • 1091 - Shortest Path in Binary Matrix
    • 1143 - Longest Common Subsequence
    • 1295 - Find Numbers with Even Number of Digits
    • 1346 - Check if N and its Double Exist
    • 1446 - Consecutive Characters
Powered by GitBook
On this page
  • Examples
  • Constraints
  • Java Solution
  1. leetcode

0617 - Merge Two Binary Trees

Previous0583 - Delete Operation for Two StringsNext0653 - Two Sum IV - Input is a BST

Last updated 3 years ago

You are given two binary trees root1 and root2.

Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.

Return the merged tree.

Note: The merging process must start from the root nodes of both trees.

Examples

Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7] Output: [3,4,5,5,4,null,7]

Input: root1 = [1], root2 = [1,2] Output: [2,2]

Constraints

The number of nodes in both trees is in the range [0, 2000]. -104 <= Node.val <= 104

Java Solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null) return root2;
        if(root2 == null) return root1;
        
        root1.val += root2.val;
        root1.left = mergeTrees(root1.left, root2.left);
        root1.right = mergeTrees(root1.right, root2.right);
        
        return root1;
    }
}
0617 - Merge Two Binary Trees