class Solution { /** * @param TreeNode $root * @return TreeNode */ function invertTree($root) { if ($root->left || $root->right) { $left = $this->invertTree($root->left); $right = $this->invertTree($root->right); $root->left = $right; $root->right = $left; } return $root; } //这个也对 function invertTree2($root) { if (!$root) { return; } $left = $this->invertTree($root->left); $right = $this->invertTree($root->right); printf("root:%d,left:%d,right:%d\n", $root->val, $left->val, $right->val); $root->left = $right; $root->right = $left; return $root; } }
copy success
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
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