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

  • 相关阅读:
    Java实现异步编程的8种方式
    纯手写 模态框、消息弹框、呼吸灯
    c++中的左值引用和右值引用
    EL表达式 & 过滤器 & 监听器
    题解2023.5.23(欧拉筛)
    三.Postman接口实践
    网络安全/黑客技术(0基础入门到进阶提升)
    【C+】C++11 —— 线程库
    紫光展锐6nm国产5G处理器T820_国产手机芯片5G方案
    网关及其分类
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126329917