226. 翻转二叉树

链接 (opens new window) 20240221192420_image.png

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