You are viewing a single comment's thread. Return to all comments →
what does the "temp = root " does here in the end
if temp.right==None and temp.left==None:
temp=root <---this one
Not my code but I did something similar.
Root is stored and never changes, so you can go back there after you reach a leaf. The variable "temp" stores the current position as you traverse down towards a leaf.
Once you reach a leaf, identified by the lack of left/right links, you retrieve the data and add it to your string. Then you need to reset your position to the root node, so you set temp=root.
temp here is obviously a poor variable name choice. The variable head should be left untouched and a new variable, forexample current_node or just node should be used to traverse the tree down.
return not node.left and not node.right
# Enter your code here. Read input from STDIN. Print output to STDOUT
def decodeHuff(root , s):
result = 
node = root
for char in s:
if char == '1':
node = go_right(node)
elif char == '0':
node = go_left(node)
node = root
I don't see how exactly your solution is different. Additionaly, temp has a local scope and leaves the root un bothered in the original solution. I can't really see how your solution improved the former