码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode-617. Merge Two Binary Trees [C++][Java]


    LeetCode-617. Merge Two Binary TreesLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/merge-two-binary-trees/

    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.

    Example 1:

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

    Example 2:

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

    Constraints:

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

     

    【C++】

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
    15. TreeNode* root = new TreeNode();
    16. if (root1 && root2) {
    17. root->val = root1->val + root2->val;
    18. root->left = mergeTrees(root1->left, root2->left);
    19. root->right = mergeTrees(root1->right, root2->right);
    20. } else if (root1) {
    21. root->val = root1->val;
    22. root->left = mergeTrees(root1->left, nullptr);
    23. root->right = mergeTrees(root1->right, nullptr);
    24. } else if (root2) {
    25. root->val = root2->val;
    26. root->left = mergeTrees(nullptr, root2->left);
    27. root->right = mergeTrees(nullptr, root2->right);
    28. } else {
    29. return nullptr;
    30. }
    31. return root;
    32. }
    33. };

    【Java】

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
    18. TreeNode root = new TreeNode();
    19. if (root1 != null && root2 != null) {
    20. root.val = root1.val + root2.val;
    21. root.left = mergeTrees(root1.left, root2.left);
    22. root.right = mergeTrees(root1.right, root2.right);
    23. } else if (root1 != null) {
    24. root.val = root1.val;
    25. root.left = mergeTrees(root1.left, null);
    26. root.right = mergeTrees(root1.right, null);
    27. } else if (root2 != null) {
    28. root.val = root2.val;
    29. root.left = mergeTrees(null, root2.left);
    30. root.right = mergeTrees(null, root2.right);
    31. } else {
    32. return null;
    33. }
    34. return root;
    35. }
    36. }

  • 相关阅读:
    TiDB 悲观事务模式
    当10年程序员是什么体验?存款几位数?
    【C语言】符号的深度理解
    银发经济崭露头角:海外网红营销如何助力假发品牌增长
    TensorFlow学习(4) 学习率调度 & 正则化
    Taichi 加速 Python 中图像处理
    MySQL进阶(数据库引擎)——MyISAM和InnoDB引擎的区别
    选择适合您网站的SSL证书,保障安全与信任
    基于webapi的websocket聊天室(番外二)
    2023NOIP A层联测9-天竺葵
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126329917
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号