why my golang code wrong, someone help me


  • 0
    W
    var myprev *TreeNode
    
    func flatten(root *TreeNode) {
    	if root == nil {
    		return
    	}
    	flatten(root.Right)
    	flatten(root.Left)
    	root.Right = myprev
    	root.Left = nil
    	myprev = root
    }
    

    Submission Result: Wrong Answer
    Input:[1,2]
    Output:[1,null,2,null,0]
    Expected:[1,null,2]

    how could I output a node val 0


  • 0
    D

    Your usage of myprev as global variable may cause its value changed when you trigger flatten func twice.
    Could you put a fmt.Println function to debug.


  • 0
    D

    I try it with java/C++ version and it works fine. Not sure it is because of golang problem.
    In Golang, checking root == nil is not enough, since address of nil struct will be not nil.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.