Golang 0ms, uses a walk function
func inOrder(root *TreeNode,processFn func(*TreeNode)) {
  if root == nil {
    return
  }
  inOrder(root.Left,processFn)
  processFn(root)
  inOrder(root.Right,processFn)
}

func increasingBST(root *TreeNode) *TreeNode {
  var newRoot *TreeNode
  rootRef := newRoot
  inOrder(root,func(r *TreeNode) {
    if newRoot == nil {
      newRoot = r
      rootRef = newRoot
    } else {
      newRoot.Right = &TreeNode{Val:r.Val}
      newRoot = newRoot.Right
    }
  })
  return rootRef
}
Comments (0)