-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmirror_of_binary_tree.php
58 lines (51 loc) · 1.17 KB
/
mirror_of_binary_tree.php
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
<?php
/**
* 剑指 Offer 系列:求一个二叉树的镜像
* Author:学院君
*/
require_once './binary_tree_node.php';
/**
* 输出一棵二叉树,返回这棵二叉树的镜像
* @param BinaryTreeNode $node
*/
function mirrorTree(BinaryTreeNode $node)
{
if ($node == null) {
return;
}
if ($node->left == null && $node->right == null) {
return;
}
$temp = $node->left;
$node->left = $node->right;
$node->right = $temp;
if ($node->left) {
mirrorTree($node->left);
}
if ($node->right) {
mirrorTree($node->right);
}
}
// 测试代码
$root = new BinaryTreeNode();
$root->data = 8;
$left_1 = new BinaryTreeNode();
$left_1->data = 10;
$right_1 = new BinaryTreeNode();
$right_1->data = 6;
$root->left = $left_1;
$root->right = $right_1;
$left_1_1 = new BinaryTreeNode();
$left_1_1->data = 5;
$left_1_2 = new BinaryTreeNode();
$left_1_2->data = 7;
$left_1->left = $left_1_1;
$left_1->right = $left_1_2;
$right_1_1 = new BinaryTreeNode();
$right_1_1->data = 9;
$right_1_2 = new BinaryTreeNode();
$right_1_2->data = 11;
$right_1->left = $right_1_1;
$right_1->right = $right_1_2;
mirrorTree($root);
var_dump($root);