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; } }
copy success
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17