版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412839
循环遍历左子树,不停的把左子树对接到右子树上去
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