首页 > 其他分享 >leetcode - 617. 合并二叉树

leetcode - 617. 合并二叉树

时间:2022-11-03 13:57:01浏览次数:72  
标签:right cur1 617 stack 二叉树 null root1 leetcode left

617. 合并二叉树

class Solution {
    //迭代
    public TreeNode mergeTreesWithStack(TreeNode root1, TreeNode root2) {
        //如果当前root左右子树有一个是空的, 直接把非空的那个返回
        if(root1 == null || root2 == null){
            return root1 == null ? root2 : root1;
        }
        Stack<TreeNode> stack = new Stack<>();
            stack.push(root1);
            stack.push(root2);
            while(!stack.isEmpty()){
                //栈是先进后出的, 后加的2, 所以先出2
                TreeNode cur2 = stack.pop();
                TreeNode cur1 = stack.pop();
                //直接把cur2的值累加到cur1上, 合并当前节点
                cur1.val += cur2.val;
                //假如当前节点左右子树都有才能放到栈里
                if(cur1.left != null && cur2.left != null){
                    stack.push(cur1.left);
                    stack.push(cur2.left);
                }else if(cur1.left == null){ //如果有1为空, 把2非空的那个直接挂到1空的节点上
                    cur1.left = cur2.left;
                }
                if(cur1.right != null && cur2.right != null){
                    stack.push(cur1.right);
                    stack.push(cur2.right);
                }else if(cur1.right == null){
                    cur1.right = cur2.right;
                }
            }
            return root1;
    }

    //递归
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        //如果1为空, 直接返回直接2非空的那个, 挂到root1上
        if(root1 == null){
            return root2;
        }
        //如果2为空, 直接返回root1, 没节点挂到root1上, 还是原来的节点
        if(root2 == null){
            return root1; 
        }
        //合并当前的头
        root1.val += root2.val;
        //合并左右子树的
        root1.left = mergeTrees(root1.left, root2.left);
        root1.right = mergeTrees(root1.right, root2.right);

        //返回合并的头
        return root1;
    }
}

 

标签:right,cur1,617,stack,二叉树,null,root1,leetcode,left
From: https://www.cnblogs.com/phonk/p/16854231.html

相关文章

  • LeetCode221. 最大正方形-中等(Go版)
    前情提示Go语言学习者,为了便于阅读和整理,本文代码已开源放在:https://github.com/honlu/GoDailyCodehttps://gitee.com/dreamzll/GoDailyCode持续更新中,已经完成排序算......
  • Leetcode第1668题:最大重复子字符串(Maximum repeating subarray)
    解题思路题目条件字符串长度不超过100,直接从大到小枚举word的重复次数k,判断word重复次数后是否还是sequence的字串,是就直接返回重复次数k。核心代码如下:classSolution......
  • 力扣 二叉树 算法+题目 整理
    二叉树基础包括三种遍历,建树和遍历的方法。二叉树遍历力扣144,94,145二叉树前中后序遍历使用递归或者迭代空间复杂度都是o(n),而通过morris遍历则可以达到o(1),其介绍......
  • leetcode-1880-easy
    CheckifWordEqualsSummationofTwoWordsThelettervalueofaletterisitspositioninthealphabetstartingfrom0(i.e.'a'->0,'b'->1,'c'->2,et......
  • leetcode-566-easy
    ReshapetheMatrixInMATLAB,thereisahandyfunctioncalledreshapewhichcanreshapeanmxnmatrixintoanewonewithadifferentsizerxckeepingits......
  • LeetCode刷题记录.Day4
    移除链表元素题目链接203.移除链表元素-力扣(LeetCode)classSolution{public:ListNode*removeElements(ListNode*head,intval){ListNode*varHe......
  • leetcode-104. 二叉树的最大深度
    题目描述给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,......
  • 【leetcode 952. 按公因数计算最大组件大小】【欧拉筛+并查集】
    importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;classSolution{List<Integer>list=newArrayList<>();intprimeNum=0......
  • leetcode-67. 二进制求和
    题目描述给你两个二进制字符串a和b,以二进制字符串的形式返回它们的和。示例输入:a="11",b="1"输出:"100"思路分析我们可以先将其转化为整数,相加之后再转为......
  • leetcode股票系列问题
    本文整合了一些大佬的文章加上自己的一些认识,供自己复习转载:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/solutions/8753/yi-ge-fang-fa-tuan-mie-6-d......