The answer is unique?


  • 1
    W

    Here is my python code.

    def flatten(self, root):
    	"""
    	:type root: TreeNode
    	:rtype: void Do not return anything, modify root in-place instead.
    	"""
    	tmp, rightMost = root, root
    	while(tmp != None):
    		if(tmp.left != None):
    			while(rightMost != None and rightMost.right != None):
    				rightMost = rightMost.right
    			rightMost.right = tmp.left
    			tmp.left = None
    		tmp = tmp.right
    

    I think the answer is not unique, it just requires to convert a tree to a link list.

    If the tree is like 2<-1->3, (2,3 is the left and right child of 1 respectively),

    my program get 1->3->2, but OJ tells me that it is wrong and the correct answer is 1->2->3.

    I do not understand.

    Can anyone tell me why?

    Thanks


  • 0

    Apparently it's because the problem setter had a particular solution/order in mind and forgot to specify it. I pointed this out as well.


  • 0
    W

    Yeah, I read your discuss in that BBS, but I can not replay there, I totally agree with you. You need the hind only when you have no idea how to solve the problem. It is not for problem description or requirement.


Log in to reply
 

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