226. Invert Binary Tree 翻转二叉树
生活随笔
收集整理的這篇文章主要介紹了
226. Invert Binary Tree 翻转二叉树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
翻轉一棵二叉樹。
示例:
輸入:
4/ \2 7/ \ / \ 1 3 6 9輸出:
4/ \7 2/ \ / \ 9 6 3 1備注:
這個問題是受到 Max Howell 的 原問題 啟發的 :
遞歸
這題,比較簡單,看代碼就能看懂。
Code
def invertTree(self, root: TreeNode) -> TreeNode:if root:self.invertTree(root.left)self.invertTree(root.right)root.right, root.left = root.left, root.rightreturn root總結
以上是生活随笔為你收集整理的226. Invert Binary Tree 翻转二叉树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派没有声音
- 下一篇: 404. Sum of Left Lea