• 617. 合并二叉树


    地址:https://leetcode-cn.com/problems/merge-two-binary-trees/

    <?php
    /**
    617. 合并二叉树
    给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。
    
    你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。
    
    示例 1:
    
    输入:
    Tree 1                     Tree 2
    1                         2
    /                        / 
    3   2                     1   3
    /                              
    5                             4   7
    输出:
    合并后的树:
    3
    / 
    4   5
    /    
    5   4   7
    注意: 合并必须从两个树的根节点开始。
     */
    /**
     * Definition for a binary tree node.
     * class TreeNode {
     *     public $val = null;
     *     public $left = null;
     *     public $right = null;
     *     function __construct($value) { $this->val = $value; }
     * }
     */
    class Solution {
    
        /**
         * @param TreeNode $t1
         * @param TreeNode $t2
         * @return TreeNode
         */
        function mergeTrees($t1, $t2) {
            if($t1 == null) return $t2;
            if($t2 == null) return $t1;
    
            $t1->val += $t2->val;
            $t1->left = $this->mergeTrees($t1->left,$t2->left);
            $t1->right = $this->mergeTrees($t1->right,$t2->right);
    
            return $t1;
        }
    }
  • 相关阅读:
    windows权限维持之注册表
    mstsc痕迹清理
    内网常用爆破手法
    RDP攻击&防御
    Java SPI 机制
    mysqldump 数据库备份
    Redis分布式锁
    Seata分布式事务中间件学习和实践
    pytube
    idea github登录
  • 原文地址:https://www.cnblogs.com/8013-cmf/p/12931555.html
Copyright © 2020-2023  润新知