上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
}
};
php 解法, 执行用时: 8 ms, 内存消耗: 15.2 MB, 提交时间: 2021-05-10 16:31:41
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {
/**
* @param TreeNode $root
* @return TreeNode
*/
function mirrorTree($root) {
if ( $root == null ) return $root;
list($root->left, $root->right) = [$root->right, $root->left];
$this->mirrorTree($root->left);
$this->mirrorTree($root->right);
return $root;
}
}
golang 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2020-11-17 21:28:18
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func mirrorTree(root *TreeNode) *TreeNode {
if root == nil {
return root
}
root.Left, root.Right = root.Right, root.Left
mirrorTree(root.Left)
mirrorTree(root.Right)
return root
}
python3 解法, 执行用时: 32 ms, 内存消耗: 13.5 MB, 提交时间: 2020-11-17 21:26:44
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
if root is None:
return root
root.left, root.right = root.right, root.left
self.mirrorTree(root.left)
self.mirrorTree(root.right)
return root