python 3.x - I am trying zig zag traversal of a binary tree. its not printing 5 of the last branch, can anybody tell me why? -
there no indentation problem in code
class node: def __init__(self,key): self.data=key self.left=none self.right=none def zig_zag(root): if not root: return s=[] l=1 s.append(root) while s: q=[] if l%2==1: in reversed(s): print(i.data,end=' ') if i.left:q.append(i.left) if i.right:q.append(i.right) else: in s: print(i.data,end=' ') if i.left:q.append(i.left) if i.right:q.append(i.right) print(q) s=q l+=1 root=node(1) root.left=node(2) root.right=node(3) root.left.left=node(7) root.left.right=node(6) root.right.left=node(5) root.right.left=node(4) zig_zag(root) the output getting [1,2,3,4,6,7] instead of [1,2,3,4,5,6,7].can explain why not appending 5 last branch of tree
algorithm fine.
have made mistake in "node adding code"
setting root.right.left twice
change
root.right.left=node(5) root.right.left=node(4) to
root.right.left=node(5) root.right.right=node(4)
Comments
Post a Comment