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

  • 相关阅读:
    C++ 学习 之 成员指针
    网络数据采集-免费全网数据采集软件
    计算机硬件和软件之间的区别
    动画原理总结
    Eth - Trunk链路聚合
    【Leetcode】动态规划-647. 回文子串
    类和对象(跑路人笔记)<完>
    Spark调度核心组件之三剑客
    供应 JOSEF约瑟 跳位合位监视继电器 JZ-7GJ-S002XMC AC220V
    Spring Cloud面试题整理
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126329917