剑指 Offer 27. 二叉树的镜像

题目链接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
func mirrorTree(_ root: TreeNode?) -> TreeNode? {
if root == nil {
return root
}
var temp = root?.left
root?.left = root?.right
root?.right = temp
mirrorTree(root?.left)
mirrorTree(root?.right)
return root
}
}

Kotlin:
执行用时:132 ms, 在所有 Kotlin 提交中击败了94.87% 的用户
内存消耗:32.9 MB, 在所有 Kotlin 提交中击败了12.82% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
fun mirrorTree(root: TreeNode?): TreeNode? {
root ?: return null
var temp = root.left
root.left = root.right
root.right = temp
mirrorTree(root.left)
mirrorTree(root.right)
return root
}
}


剑指 Offer 27. 二叉树的镜像
https://pisces34.github.io/2021/11/20/leetcode/offer27/
发布于
2021年11月20日
许可协议