Leetcode Note: Go - Increasing Order Search Tree
Increasing Order Search Tree - LeetCode
https://leetcode.com/problems/increasing-order-search-tree/
- Go 言語で取り組んだメモ
回答
Increasing Order Search Tree - LeetCode
https://leetcode.com/problems/increasing-order-search-tree/solution/
Go solution - LeetCode Discuss
https://leetcode.com/problems/increasing-order-search-tree/discuss/1849038/Go-solution
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func increasingBST(root *TreeNode) *TreeNode {
return dfs(root, nil)
}
func dfs(root, tail *TreeNode) *TreeNode {
if root == nil {
return tail
}
res := dfs(root.Left, root)
root.Left = nil
root.Right = dfs(root.Right, tail)
return res
}