897. Increasing Order Search Tree
Last updated
Last updated
Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]func increasingBST(root *TreeNode) *TreeNode {
dummy := &TreeNode{}
ans := dummy
var stack []*TreeNode
for len(stack) > 0 || root != nil {
for root != nil {
stack = append(stack, root)
root = root.Left
}
root = stack[len(stack)-1]
stack = stack[:len(stack)-1]
dummy.Right = &TreeNode{Val: root.Val}
root, dummy = root.Right, dummy.Right
}
return ans.Right
}