代码随想录 617.合并二叉树

题目要求:

1.如果两个节点重叠,则两个节点值相加作为合并后节点的新值。

2.否则,不为null的节点将直接作为新二叉树的节点。

3.合并过程必须从两个树的根节点开始。

思路:同时遍历两个二叉树,其实和遍历一个树的逻辑是一样的,只不过需要传入两个树的节点同时操作。

方法一:前序遍历递归(使用中序、后序遍历也可)。

1.确定递归函数的参数和返回值:因为要合并两棵树,所以参数是两个二叉树的根节点,返回值是合并后二叉树的根节点。

java 复制代码
TreeNode mergeTrees(TreeNode root1, TreeNode root2)

2.确定终止条件:因为传入了两个树,那么就有两个树遍历的节点root1和root2。

(1)如果root1 == null,两个树合并之后就是root2;

(2)反过来如果root2 == null,两个树合并之后就是root1。

(3)如果两个树都是null,合并之后自然是null(可省略不写)。

java 复制代码
        if(root1 == null){
            return root2;
        }
        if(root2 == null){
            return root1;
        }

3.确定单层递归的逻辑:借助root1树存放合并后树的节点值。接下来root1的左子树就是合并root1和root2后的左子树;root1的右子树就是合并root1和root2后的右子树。最终root1就是合并后的根节点。

java 复制代码
        root1.val += root2.val;
        root1.left = mergeTrees(root1.left,root2.left);
        root1.right = mergeTrees(root1.right,root2.right);
        return root1;

附代码:

java 复制代码
    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;
    }
}

方法二:层序遍历迭代法

思路:把两个树的节点同时加入队列。

附代码:

java 复制代码
    class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        LinkedList<TreeNode> queue = new LinkedList<>();
        if(root1 == null) return root2;
        if(root2 == null) return root1;
        queue.add(root1);
        queue.add(root2);
        while(queue.size() > 0){
            TreeNode t1 = queue.remove();
            TreeNode t2 = queue.remove();
            //此时两个节点一定不为空,val值相加
            t1.val += t2.val;
            if(t1.left != null && t2.left != null){
                queue.add(t1.left);
                queue.add(t2.left);
            }
            if(t1.right != null && t2.right != null){
                queue.add(t1.right);
                queue.add(t2.right);
            }
            //如果t1的左节点为空,则把t2的左节点赋值过去
            if(t1.left == null && t2.left != null){
                t1.left = t2.left;
            }
            //如果t1的右节点为空,则把t2的右节点赋值过去
            if(t1.right == null && t2.right != null){
                t1.right = t2.right;
            }
        }
        return root1; //返回合并后的root1
    }
}
相关推荐
Code_Solitude16 分钟前
C语言动态内存 + 柔性数组 + 数组名退化
c语言·算法·柔性数组
上海潭龙东海科技有限公司23 分钟前
客运站管控之动态孪生技术方案(新增一屏管控模块
数据仓库·人工智能·算法·重构·动态规划
YLCHUP3 小时前
【思维&图论建模】题解:债权(debt)_图论_建模_思路转化_思维题目_C++算法竞赛
c++·笔记·算法·数学建模·贪心算法·深度优先·图论
思麟呀3 小时前
背包DP(01背包+完全背包)
c++·算法
笑容掩饰我的失落4 小时前
区块链从小白到精通(七):Solidity 语法精讲——数据类型、函数、修饰器与事件
算法·区块链·php·智能合约·哈希算法
Pniubi5 小时前
力扣24. 两两交换链表中的节点(递归)
算法·leetcode·链表
nice_lcj5205 小时前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru
꧁细听勿语情꧂6 小时前
直接选择排序,堆排序,冒泡排序,递归快速排序、hoare版本、挖坑法、lomuto前后指针,非递归快速排序
c语言·开发语言·数据结构·算法
俊俊几年的6 小时前
动态规划(Dynamic Programming)详解:从入门到 LeetCode Hot 100
linux·c++·算法·leetcode·嵌入式·动态规划·c
小雉系统15 小时前
BcoreOS安全通信架构:FsSession 抗算法后门安全设计
算法·安全·架构