Leetcode617. 两个二叉树相加

题目描述

leetcode226.翻转二叉树非常相似,核心还是递归。
答案:

    class Solution {
        public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
            if (root1 == null) {
                return root2;
            }

            if (root2 == null) {
                return root1;
            }

            TreeNode root = new TreeNode();
            root.val = root1.val + root2.val;
            root.left = mergeTrees(root1.left, root2.left);
            root.right = mergeTrees(root1.right, root2.right);
            return root;
        }
    }

相关推荐

  1. Leetcode617. 相加

    2024-07-18 13:58:01       17 阅读
  2. Leetcode100.判断是否相同

    2024-07-18 13:58:01       23 阅读
  3. 617. 合并

    2024-07-18 13:58:01       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 13:58:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 13:58:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 13:58:01       58 阅读
  4. Python语言-面向对象

    2024-07-18 13:58:01       69 阅读

热门阅读

  1. request method ‘DELETE‘ is not supported问题

    2024-07-18 13:58:01       22 阅读
  2. 【日常技能】excel 换行符替换的3个方法完美解决

    2024-07-18 13:58:01       21 阅读
  3. C# —— Sort排序

    2024-07-18 13:58:01       24 阅读
  4. centos跳过首次创建用户

    2024-07-18 13:58:01       21 阅读
  5. 使用Spring Retry实现重试机制

    2024-07-18 13:58:01       21 阅读
  6. 一行命令实现 Github 国内下载加速

    2024-07-18 13:58:01       22 阅读
  7. kotlin 退出Activity 平滑动画

    2024-07-18 13:58:01       21 阅读
  8. C语言面试题

    2024-07-18 13:58:01       21 阅读
  9. 1.1 系统架构概述

    2024-07-18 13:58:01       19 阅读
  10. live555 rtsp服务器实战之doGetNextFrame

    2024-07-18 13:58:01       22 阅读