-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-two-binary-trees_0421.html
63 lines (59 loc) · 1.68 KB
/
merge-two-binary-trees_0421.html
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
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>merge-two-binary-trees</title>
</head>
<body></body>
<script>
const t1 = [1, 3, 2, 5];
const t2 = [2, 1, 3, null, 4, null, 7];
console.log(mergeTrees(t1, t2));
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root1
* @param {TreeNode} root2
* @return {TreeNode}
*/
var mergeTrees = function (t1, t2) {
console.log("----------进入递归--------");
console.log("t1:");
console.log(t1);
console.log("t2:");
console.log(t2);
if (!t1 || !t2) {
console.log("t1 null:");
console.log(t1);
console.log("t2 null:");
console.log(t2);
console.log("t1 || t2:");
console.log(t1 || t2);
return t1 || t2;
}
t1.val += t2.val;
console.log("t1.val + t2.val:" + t1.val);
console.log("new t1:");
console.log(t1);
console.log("new t2:");
console.log(t2);
console.log("------------------------");
z;
t1.left = mergeTrees(t1.left, t2.left);
console.log("finish t1 left");
t1.right = mergeTrees(t1.right, t2.right);
console.log("finish t1 right");
console.log("t1 result:");
console.log(t1);
return t1;
};
</script>
</html>