https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/
//递归
func flatten(_ root: TreeNode?) {
if root == nil {return}
let oldRightNode = root?.right
root?.right = root?.left
root?.left = nil
var rightMost = root
while rightMost?.right != nil {
rightMost = rightMost?.right
}
rightMost?.right = oldRightNode
flatten(root?.right)
}
//非递归
func flatten(_ root: TreeNode?) {
if root == nil {return}
var mulRoot = root
while mulRoot != nil {
if mulRoot!.left != nil {
let oldRightNode = mulRoot?.right
mulRoot?.right = mulRoot?.left
mulRoot?.left = nil
var rightMost = mulRoot
while rightMost?.right != nil {
rightMost = rightMost?.right
}
rightMost?.right = oldRightNode
}
mulRoot = mulRoot?.right
}
}