Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- public class Main
- {
- static int ans = 1, index=0;
- public static class Node{
- int val;
- Node left;
- Node right;
- Node(int val) {
- this.val = val;
- this.left = null;
- this.right = null;
- }
- }
- public static Node insert(Node root, int val){
- if(root == null) {
- return new Node(val);
- }
- if(val < root.val){
- root.left = insert(root.left, val);
- }
- else
- root.right = insert(root.right, val);
- return root;
- }
- public static void inOrderTraversal(Node root , int[] arr){
- if(root == null) {
- return;
- }
- inOrderTraversal(root.left, arr);
- if(root.val != arr[index]){
- ans = 0;
- }
- index+=1;
- inOrderTraversal(root.right, arr);
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner in = new Scanner(System.in);
- int size = in.nextInt();
- int arr[] = new int[size];
- for(int i = 0; i < size; i++) {
- arr[i] = in.nextInt();
- }
- Node root = null;
- root = new Node(arr[0]);
- for(int i = 1; i < size; i++) {
- root = insert(root, arr[i]);
- }
- inOrderTraversal(root, arr);
- System.out.print(ans);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement