題目
請完成一個函數,輸入一個二叉樹,該函數輸出它的鏡像。
例如輸入:
4
/ \
2 7
/ \ / \
1 3 6 9
鏡像輸出:
4
/ \
7 2
/ \ / \
9 6 3 1
示例 1:
輸入:root = [4,2,7,1,3,6,9]
輸出:[4,7,2,9,6,3,1]
限制:
0 <= 節點個數 <= 1000
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
解法
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
if root is None: return None
root.left, root.right = self.mirrorTree(root.right), self.mirrorTree(root.left)
return root
總結
遞歸大法好,python大法好。
以后有空看看棧和隊列。