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
|
447 Discussions
|
Please Login in order to post a comment
Very Simple Javascript Recursive Solution
const resp = []
function helperFunc(root) { if(!root) return; resp.push(root.data) helperFunc(root.left); helperFunc(root.right); }
function preOrder(root) { helperFunc(root); console.log(resp.join(' ')); }
Very Simple Javascript Recursive Solution
const resp = []
function helperFunc(root) { if(!root) return; resp.push(root.data) helperFunc(root.left); helperFunc(root.right); }
function preOrder(root) { helperFunc(root); console.log(resp.join(' ')); }
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.