Table of Contents

# Tree: Preorder Traversal – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

# Solutions of Algorithms Data Structures Hard HackerRank:

## Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts

# 1 Month Preparation Kit Solutions – HackerRank

# C++ Tree: Preorder Traversal HackerRank Solution

#include <bits/stdc++.h> using namespace std; class Node { public: int data; Node *left; Node *right; Node(int d) { data = d; left = NULL; right = NULL; } }; class Solution { public: Node* insert(Node* root, int data) { if(root == NULL) { return new Node(data); } else { Node* cur; if(data <= root->data) { cur = insert(root->left, data); root->left = cur; } else { cur = insert(root->right, data); root->right = cur; } return root; } } void preOrder(Node *root) { if(root==NULL) { return; } printf("%d ",root->data); preOrder(root->left); preOrder(root->right); } }; //End of Solution int main() { Solution myTree; Node* root = NULL; int t; int data; std::cin >> t; while(t-- > 0) { std::cin >> data; root = myTree.insert(root, data); } myTree.preOrder(root); return 0; }

# Java Tree: Preorder Traversal HackerRank Solution

import java.util.*; import java.io.*; class Node { Node left; Node right; int data; Node(int data) { this.data = data; left = null; right = null; } } class Solution { public static void preOrder(Node root) { System.out.print(root.data + " "); if (root.left != null) { preOrder(root.left); } if (root.right != null) { preOrder(root.right); } } public static Node insert(Node root, int data) { if(root == null) { return new Node(data); } else { Node cur; if(data <= root.data) { cur = insert(root.left, data); root.left = cur; } else { cur = insert(root.right, data); root.right = cur; } return root; } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); Node root = null; while(t-- > 0) { int data = scan.nextInt(); root = insert(root, data); } scan.close(); preOrder(root); } }

# Python 3 Tree: Preorder Traversal HackerRank Solution

class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None def __str__(self): return str(self.info) class BinarySearchTree: def __init__(self): self.root = None def create(self, val): if self.root == None: self.root = Node(val) else: current = self.root while True: if val < current.info: if current.left: current = current.left else: current.left = Node(val) break elif val > current.info: if current.right: current = current.right else: current.right = Node(val) break else: break def preOrder(root): #Write your code here p = root if p is None: return print(p.info, end=' ') preOrder(p.left) preOrder(p.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 Tree: Preorder Traversal HackerRank Solution

# C Tree: Preorder Traversal HackerRank Solution

#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> struct node { int data; struct node *left; struct node *right; }; struct node* insert( struct node* root, int data ) { if(root == NULL) { struct node* node = (struct node*)malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return node; } else { struct node* cur; if(data <= root->data) { cur = insert(root->left, data); root->left = cur; } else { cur = insert(root->right, data); root->right = cur; } return root; } } void preOrder( struct node *root) { if (root != NULL) { printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } } int main() { struct node* root = NULL; int t; int data; scanf("%d", &t); while(t-- > 0) { scanf("%d", &data); root = insert(root, data); } preOrder(root); return 0; }

## Leave a comment below