思路
循环遍历左子树,不停的把左子树对接到右子树上去
code
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func flatten(root *TreeNode) {
node := root
for node != nil {
if node.Left != nil {
tmp := node.Left
for tmp.Right != nil {
tmp = tmp.Right
}
tmp.Right = node.Right
node.Right = node.Left
node.Left = nil
}
node = node.Right
}
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode