We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Tree: Preorder Traversal
Tree: Preorder Traversal
Sort by
recency
|
442 Discussions
|
Please Login in order to post a comment
class Node: def init(self,val): self.value=val self.left=None self.right=None
class Tree: def init(self): self.root=None
n=int(input()) nums=list(map(int,input().split())) tree=Tree() tree.root=Node(nums[0]) for num in nums[1:]: tree.insert(tree.root,num) tree.preOrder(tree.root)
There is a gotcha here, that the C# template doesn't parse the input like the languages. And the description doesn't describe how to parse the input.
def preOrder(root): if(root==None): return 0 print(root.info, end=" ") preOrder(root.left) preOrder(root.right)
JAVA: