leetcode-114-二叉树展开为链表*
生活随笔
收集整理的這篇文章主要介紹了
leetcode-114-二叉树展开为链表*
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目描述:
?
方法一:迭代
class Solution:def flatten(self, root: TreeNode) -> None:"""Do not return anything, modify root in-place instead."""cur = root while cur: if cur.left: p = cur.left while p.right: p = p.right p.right = cur.right cur.right = cur.left cur.left = None cur = cur.right?方法二:遞歸
class Solution:def flatten(self, root: TreeNode) -> None:"""Do not return anything, modify root in-place instead."""def helper(root, pre): if not root: return pre # 記錄遍歷時(shí)候,該節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn) pre = helper(root.right, pre) pre = helper(root.left, pre) # 拼接 root.right = pre root.left = None pre = root return pre helper(root, None)?
轉(zhuǎn)載于:https://www.cnblogs.com/oldby/p/11185508.html
總結(jié)
以上是生活随笔為你收集整理的leetcode-114-二叉树展开为链表*的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。