/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func flatten(root *TreeNode) {
nodes :=PreOrder(root)
i:=1
p :=root
root.Left =nil
for ;i<len(nodes);i++{
p.Left =nil
p.Right = nodes[i]
p = p.Right
}
}
func PreOrder(node *TreeNode) []*TreeNode{
var nodes []*TreeNode
if node !=nil{
nodes = append(nodes,node)
//....
nodes = append(nodes,PreOrder(node.Left)...)
nodes = append(nodes,PreOrder(node.Right)...)
}
return nodes
}
二叉树展开为链表 golang
最新推荐文章于 2024-10-31 14:39:50 发布