• Leetcode 617. Merge Two Binary Trees


    题目链接:https://leetcode.cn/problems/merge-two-binary-trees/

    参考链接:https://programmercarl.com/0617.%E5%90%88%E5%B9%B6%E4%BA%8C%E5%8F%89%E6%A0%91.html#%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E7%89%88%E6%9C%AC

    方法一 递归-前序遍历

    1 方法思想

    2 代码实现

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

    3 复杂度分析

    时间复杂度:
    空间复杂度:

    4 涉及到知识点

    5 总结

    方法一 栈

    1 方法思想

    2 代码实现

    class Solution {
        // 使用栈迭代
        public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
            if (root1 == null) {
                return root2;
            }
            if (root2 == null) {
                return root1;
            }
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root2);
            stack.push(root1);
            while (!stack.isEmpty()) {
                TreeNode node1 = stack.pop();
                TreeNode node2 = stack.pop();
                node1.val += node2.val;
                if (node2.right != null && node1.right != null) {
                    stack.push(node2.right);
                    stack.push(node1.right);
                } else {
                    if (node1.right == null) {
                        node1.right = node2.right;
                    }
                }
                if (node2.left != null && node1.left != null) {
                    stack.push(node2.left);
                    stack.push(node1.left);
                } else {
                    if (node1.left == null) {
                        node1.left = node2.left;
                    }
                }
            }
            return root1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    3 复杂度分析

    时间复杂度:
    空间复杂度:

    4 涉及到知识点

    5 总结

    方法三 队列

    1 方法思想

    2 代码实现

    class Solution {
        // 使用队列迭代
        public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
            if (root1 == null) return root2;
            if (root2 ==null) return root1;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root1);
            queue.offer(root2);
            while (!queue.isEmpty()) {
                TreeNode node1 = queue.poll();
                TreeNode node2 = queue.poll();
                // 此时两个节点一定不为空,val相加
                node1.val = node1.val + node2.val;
                // 如果两棵树左节点都不为空,加入队列
                if (node1.left != null && node2.left != null) {
                    queue.offer(node1.left);
                    queue.offer(node2.left);
                }
                // 如果两棵树右节点都不为空,加入队列
                if (node1.right != null && node2.right != null) {
                    queue.offer(node1.right);
                    queue.offer(node2.right);
                }
                // 若node1的左节点为空,直接赋值
                if (node1.left == null && node2.left != null) {
                    node1.left = node2.left;
                }
                // 若node2的左节点为空,直接赋值
                if (node1.right == null && node2.right != null) {
                    node1.right = node2.right;
                }
            }
            return root1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    3 复杂度分析

    时间复杂度:
    空间复杂度:

    4 涉及到知识点

    5 总结

  • 相关阅读:
    C#学习记录——在C#中操作注册表
    Prism进入视图时导航的三种方式
    ES6 入门教程 10 对象的扩展 10.1 属性的简洁表示法
    中间件 | Redis - [redis]
    istio实战:springboot项目在istio中服务调用
    BigCode 背后的大规模数据去重
    汇舟问卷:想要挣钱?海外问卷调查不容错过!
    Golang GMP调度模型:实现高效协程调度和执行
    lambda表达式
    基于脉冲神经网络的物体检测
  • 原文地址:https://blog.csdn.net/weixin_42542290/article/details/127855441