-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeTwoBinaryTrees.php
More file actions
51 lines (40 loc) · 1.2 KB
/
mergeTwoBinaryTrees.php
File metadata and controls
51 lines (40 loc) · 1.2 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* Definition for a binary tree node.
*
*/
class TreeNode
{
public $val = null;
public $left = null;
public $right = null;
function __construct($val = 0, $left = null, $right = null)
{
$this->val = $val;
$this->left = $left;
$this->right = $right;
}
}
/**
* 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。
* 你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/merge-two-binary-trees
* @param TreeNode $root1
* @param TreeNode $root2
* @return TreeNode
*/
function mergeTrees($root1, $root2)
{
if ($root1 === null) {
return $root2;
}
if ($root2 === null) {
return $root1;
}
$node = new TreeNode();
$node->val = $root1->val + $root2->val;
$node->left = mergeTrees($root1->left, $root2->left);
$node->right = mergeTrees($root1->right, $root2->right);
return $node;
}