/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcflatten(root*TreeNode){varprev*TreeNodevarpreorderfunc(root*TreeNode)preorder=func(root*TreeNode){ifnil==root{return}ifprev!=nil{prev.Left=nilprev.Right=root}prev=rootleft,right:=root.Left,root.Rightpreorder(left)preorder(right)}preorder(root)}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcflatten(root*TreeNode){cur:=rootforcur!=nil{mostRight:=cur.LeftifmostRight!=nil{formostRight.Right!=nil{mostRight=mostRight.Right}mostRight.Right=cur.Rightcur.Left,cur.Right=nil,cur.Leftcur=cur.Right}else{cur=cur.Right}}}