Java traversal binary tree: preorder traversal, middle order traversal, post-order traversal, traversal depth, number of leaf no

import java.util.ArrayDeque;
Import java.util.Queue;
 
Public class CreateTree {
 
/**
* @param args
*/
Public static void main(String[] args) {
// TODO Auto-generated method stub
Node root = new Node();
Root.data = 9;
 
Node temp01 = new Node();
Temp01.data = 1;
Root.left = temp01;
 
Node temp02 = new Node();
Temp02.data = 3;
Root.right = temp02;
 
Node temp03 = new Node();
Temp03.data = 2;
Root.left.left = temp03;
 
Node temp04 = new Node();
Temp04.data = 4;
Root.left.right = temp04;
 
Node temp05 = new Node();
Temp05.data = 8;
Root.right.left = temp05;
 
Node temp06 = new Node();
Temp06.data = 6;
Root.left.left.left = temp06;
 
Node temp07 = new Node();
Temp07.data = 7;
Root.left.left.right = temp07;
 
System.out.println("--------preorder traversal----------");
SelectTree1(root);
System.out.println();
System.out.println("---------Intermediate traversal ---------");
SelectTree(root);
System.out.println();
System.out.println("---------post-order traversal ---------");
SelectTree2(root);
System.out.println();
System.out.println("----------Number of leaf nodes-----------");
Int i = leafNum(root);
System.out.println(i);
System.out.println("----------level traversal binary tree-----------------");
levelOrder(root);
 
System.out.println();
Int j = deep(root);
System.out.println("---------height---------");
System.out.println(j);
 
}
 
// in-order traversal
Public static void SelectTree(Node root) {
If (root == null)
Return;
SelectTree(root.left);
System.out.print(root.data + " ");
SelectTree(root.right);
}
 
// Preorder traversal
Public static void SelectTree1(Node root) {
If (root == null)
Return;
System.out.print(root.data + " ");
SelectTree1(root.left);
SelectTree1(root.right);
}
 
// Post-order traversal
Public static void SelectTree2(Node root) {
If (root == null)
Return;
SelectTree2(root.left);
SelectTree2(root.right);
System.out.print(root.data + " ");
 
}
 
// number of leaves
Public static int leafNum(Node node) {
If (node ​​!= null) {
If (node.left == null && node.right == null) {
Return 1;
}
Return leafNum(node.left) + leafNum(node.right);
}
Return 0;
}
 
// Find the depth of the binary tree
Public static int deep(Node node) {
Int h1, h2;
If (node ​​== null) {
Return 0;
} else {
H1 = deep(node.left);
H2 = deep(node.right);
Return (h1 < h2) ? h2 + 1 : h1 + 1;
}
 
}
 
// Hierarchical traversal
Public static void levelOrder(Node node) {
If (node ​​== null)
Return;
Queue<Node> queue = new ArrayDeque<Node>();
Queue.add(node);
While (!queue.isEmpty()) {
Node temp = queue.poll();
System.out.print(temp.data);
If (temp.left != null)
Queue.add(temp.left);
If (temp.right != null)
Queue.add(temp.right);
}
}
}
 
Class Node {
Boolean visited = false;
Int data = 0;
Node left = null;
Node right = null;
}