• 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. }

  • 相关阅读:
    Confluence的安装部署
    Python编程 字符串组成方式
    python二级题库 第四套 附刷题软件
    单目标优化算法:遗传算法
    股指期货基差和升贴水介绍
    Java项目源码SSM农场信息管理系统
    基于SSM的鲜花商城系统【附源码文档】
    新手看过来----代码自测通过但作业通不过
    IDEA 2022 创建 Maven Web 项目教程
    【HarmonyOS】应用振动效果实现
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126329917