Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 ) {
- if( root == null)
- return;
- System.out.print(root.data + " ");
- preOrder(root.left);
- preOrder(root.right);
- }
- /* Node is defined as :
- class Node
- int data;
- Node left;
- Node right;
- */
- public static Node insert(Node root,int data) {
- if(root == null){
- Node newNode = new Node(data);
- return newNode;
- }else if(data<=root.data){
- root.left = insert(root.left,data);
- return root;
- }else {
- root.right = insert(root.right,data);
- return root;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement