Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive preorder traversal of binary tree #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/pkg1/Aryansh_Class.class
Binary file not shown.
46 changes: 46 additions & 0 deletions src/pkg1/Aryansh_Class.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package pkg1;
public class Aryansh_Class {
public TreeNode root;

public class TreeNode {
int val;
TreeNode left;
TreeNode right;

TreeNode(int val){
this.val = val;
}
}

public void createBinaryTree(){
TreeNode first = new TreeNode(9);
TreeNode second = new TreeNode(2);
TreeNode third = new TreeNode(4);
TreeNode fourth = new TreeNode(8);
TreeNode fifth = new TreeNode(1);
TreeNode sixth = new TreeNode(6);
TreeNode seventh = new TreeNode(7);

root = first;
first.left = second;
first.right = third;

second.left = fourth;
second.right = fifth;

third.left = sixth;
third.right = seventh;

}

public void preorder(TreeNode root){
if(root == null)
return;

System.out.print(root.val + " ");
preorder(root.left);
preorder(root.right);
}


}
3 changes: 3 additions & 0 deletions src/pkg1/MasterClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ public class MasterClass {
public static void main(String[] args) {
Hitesh_Class hks = new Hitesh_Class();
hks.sayHello();
Aryansh_Class ars = new Aryansh_Class();
ars.createBinaryTree();
ars.preorder(ars.root);
}
}