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
|
445 Discussions
|
Please Login in order to post a comment
class Node: def init(self, info): self.info = info
self.left = None
self.right = None self.level = None
class BinarySearchTree: def init(self): self.root = None
""" Node is defined as self.left (the left child of the node) self.right (the right child of the node) self.info (the value of the node) """ def preOrder(root): if(root==None): return 0 print(root.info, end=" ") preOrder(root.left) preOrder(root.right)
tree = BinarySearchTree() t = int(input())
arr = list(map(int, input().split()))
for i in range(t): tree.create(arr[i])
preOrder(tree.root)
JavaScript solution using recursion
Haskell
It would have been nice if the problem statement gave an actual sample of the expected input data and how to parse it... Bad form, folks.
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.