Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

In-order traversal in Go is not working correctly

I am trying to solve this problem in leetcode using Go. The following code is not working. Please help me understand what I am missing.

https://leetcode.com/problems/increasing-order-search-tree/

func increasingBST(root *TreeNode) *TreeNode {

    var newRoot = TreeNode{Val:0}
    var currentNode = TreeNode{Val:0}
    recur(root, &currentNode, &newRoot)
    return &newRoot
}

func recur(root *TreeNode, currentNode *TreeNode, newRoot *TreeNode) {
    if root == nil {
        return
    }

    recur(root.Left, currentNode, newRoot)
    if newRoot.Val == 0 {
        currentNode = root
        newRoot = currentNode
        root.Left = nil
    } else {
        root.Left = nil
        currentNode.Right = root
        currentNode = currentNode.Right
    }
    recur(root.Right, currentNode, newRoot)
}

Comments