Leetcode PHP题解–D32 617. Merge Two Binary Trees

6次阅读

共计 836 个字符,预计需要花费 3 分钟才能阅读完成。

617. Merge Two Binary Trees
题目链接
617. Merge Two Binary Trees
题目分析
给定两个二叉树,返回一个 将对应位置值相加后的二叉树。
例如,树 A 的顶点值为 1,树 B 的顶点值为 2,那么返回的二叉树的顶点值需要是 3。
思路
顶点自然不用多说,直接相加就可以了。
按照习惯,先遍历左节点。如果树 A 和树 B 都有左节点,那么直接相加,再递归当前函数去判断左节点的左节点。
若树 A 和树 B 任意一棵树没有左节点时,直接把有左节点迁移过来即可。因为,如果没有左节点,不可能会有左节点的左节点,或左节点的右节点。因此,直接照搬过来就可以了。
若两颗树都没有左节点时,忽略,直接去算右节点,并遵从以上规则即可。
最终代码
<?php
/**
* 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 {
function mergeTrees($t1, $t2) {
if(is_null($t1->val)&&is_null($t2->val)){
return;
}
$t1->val += $t2->val;
if($t1->left&&$t2->left){
$this->mergeTrees($t1->left, $t2->left);
}
if(!$t1->left&$t2->left){
$t1->left = $t2->left;
}
if($t1->right && $t2->right){
$this->mergeTrees($t1->right, $t2->right);
}
if(!$t1->right&&$t2->right){
$t1->right = $t2->right;
}
return $t1;
}
}
若觉得本文章对你有用,欢迎用爱发电资助。

正文完
 0