操操操

恢复一棵BST二叉搜索树golang版本

2023-08-20
1分钟阅读时长

恢复一棵BST二叉搜索树golang版本

type TreeNode struct{
	Val int
	Left *TreeNode
	Right *TreeNode
}

var x,y,parent *TreeNode

func recoveryTree(root *TreeNode){
	x,y,parent = nil,nil,nil
	traverse(root)
	x.Val,y.Val = y.Val,x.Val
}

func traverse(root *TreeNode){
	if root == nil{
		return
	}
	traverse(root.Left)
	if root != nil && parent.Val > root.Val{
		y = root 
		if x == nil{
			x = parent
		}else{
			return 
		}
		parent = root
		traverse(root.Right)
	}
}
Avatar

Aisen

Be water,my friend.