leetcode.617 合并两个二叉树

题目描述:给予两个二叉树  t1 , t2  ,合并他们。

Given two binary trees and 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 them 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 new tree.

Example 1:

Input:
	Tree 1                     Tree 2
          1                         2
         / \                       / \
        3   2                     1   3
       /                           \   \
      5                             4   7
Output:
Merged tree:
	     3
	    / 	   4   5
	  / \   \
	 5   4   7

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

思路:

1、输入t1 t2,判断是否都为null  ,如果是 则返回null

2、判断 t1 t2 是否都不为null  ,如果是 则 t1.val += t2.val

3、否则判断 t1 == null && t2 != null ,如果是 则 t1 = new TreeNode(t2.val)

4、如果 t2 !=null 那么说明t2可能还有子节点可以合并到 t1 ,于是进行递归

t1.left = mergeTrees(t1.left, t2.left)

t1.right = mergeTrees(t1.right, t2.right)

5、返回t1

每次递归都是只针对一个节点的合并

完整代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {

        // 思路
        // 1. 输入t1 t2 判断是否都为null,如果是则返回null
        // 2. 如果t1 t2 都不为null  那么t1.val += t2.val
        // 3. 如果t1 == null  t2!=null 那么就在t1创建一个节点,值为t2.val
        // 4. 如果t2 !=null 那么就进行递归,
        //    t1.left = mergeTrees(t1.left, t2.left)
        //    t1.right = mergeTrees(t1.right, t2.right)
        // 5. 返回t1

        if(t1 == null && t2 == null)
            return null;
        if(t1 != null && t2 != null){
            t1.val += t2.val;
        }else if(t1 == null && t2 != null){
            t1 = new TreeNode(t2.val);
        }
        if(t2 !=null){
            t1.left = mergeTrees(t1.left, t2.left);
            t1.right = mergeTrees(t1.right, t2.right);
        }
        return t1;

    }
}

原文地址:https://www.cnblogs.com/smallone/p/12117869.html

时间: 2024-10-08 19:27:35

leetcode.617 合并两个二叉树的相关文章

leetcode算法题2: 合并两个二叉树。递归,如何切入并保持清醒?

/* Given two binary trees and 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 them into a new binary tree. The merge rule is that if two nodes overlap, t

617.Merge Two Binary Trees 合并两个二叉树

Given two binary trees and 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 them into a new binary tree. The merge rule is that if two nodes overlap, then

Leetcode 617.合并二叉树

合并二叉树 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点. 示例 1: 输入: 输出: 合并后的树: 注意: 合并必须从两个树的根节点开始. 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int

LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode21. Merge Two Sorted Lists 示例: 输入: 1->2->4, 1->3->4 输出: 1->1->2->3->4->4 Java 实现 ListNode 类 class ListNode { int val; ListNode n

[leetcode] 21. 合并两个有序链表

21. 合并两个有序链表 两个有序链表合并为一个新的有序链表 class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode ans = new ListNode(Integer.MAX_VALUE); ListNode p = ans; while (l1 != null && l2 != null) { if (l1.val < l2.val) { p.next = l1; l

[leetcode] 88. 合并两个有序数组

88. 合并两个有序数组 水题,没有在原数组上做,偷了个懒 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] ans = new int[m + n]; int i = 0, j = 0; int top = 0; while (i < m && j < n) { if (nums1[i] < nums2[j]) { ans[top++] = nums1[i

领扣(LeetCode)合并两个有序数组 个人题解

给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m 和 n. 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素. 示例: 输入: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 输出: [1,2,2,3,5,6] 这道题,第一想法是动态插入,但是种种错误

【Leetcode】合并两个有序链表

题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 题解链接 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NUL

[leetcode 88. 合并两个有序数组] 尾部归并O(1)空间,最好O(n)最坏O(m+n),双100%

题目描述 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m 和 n . 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素. 示例: 输入: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 输出: [1,2,2,3,5,6] 解题思路 题目比较简单,