Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // program to print the right view of a given binary tree
- /**
- * Created by MOHIT on 25-05-2018.
- */
- import java.util.*;
- import static java.lang.Integer.max;
- // node class
- class node{
- int data;
- node left;
- node right;
- // function that returns a pointer to new node
- public node(int element){
- this.data = element;
- this.left = null;
- this.right = null;
- }
- };
- public class BinaryTree {
- static int max_level = -1;
- // function to print the right view of the binary tree
- static void right_view(node Node, int level){
- if (Node == null)
- return;
- if (max_level < level){
- max_level = level;
- System.out.print(Node.data + " ");
- }
- right_view(Node.right, level+1);
- right_view(Node.left, level+1);
- }
- public static void main(String arg[]) {
- node head = new node(1);
- head.left = new node(2);
- head.right = new node(3);
- head.left.left = new node(4);
- head.left.right = new node(5);
- head.right.right = new node(6);
- head.left.left.right = new node(7);
- head.right.right.left = new node(8);
- head.left.left.right.left = new node(9);
- head.left.left.right.left.left = new node(10);
- head.right.right.left.right = new node(11);
- System.out.print("Right view of the binary tree is : ");
- right_view(head, 0);
- }
- }
Add Comment
Please, Sign In to add comment