From 787b2e937107319e9ef890156e521a1fe571a089 Mon Sep 17 00:00:00 2001 From: Suraj Kumar Modi <76468931+skmodi649@users.noreply.github.com> Date: Mon, 25 Oct 2021 18:53:35 +0530 Subject: [PATCH] chore(Java): add check tree traversal (#609) --- algorithms/Java/README.md | 1 + .../Java/trees/check-tree-traversal.java | 137 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 algorithms/Java/trees/check-tree-traversal.java diff --git a/algorithms/Java/README.md b/algorithms/Java/README.md index af50d9cf..c3729392 100644 --- a/algorithms/Java/README.md +++ b/algorithms/Java/README.md @@ -93,6 +93,7 @@ - [Right View of a Tree](trees/right-view.java) - [Zig-Zag Traversal of a Tree](trees/zig-zag-traversal.java) - [Min Heap](trees/MinHeap.java) +- [Check Tree Traversal](trees/check-tree-traversal.java) ## Backtracking - [N Queen Problem](backtracking/nqueen.java) diff --git a/algorithms/Java/trees/check-tree-traversal.java b/algorithms/Java/trees/check-tree-traversal.java new file mode 100644 index 00000000..22875f27 --- /dev/null +++ b/algorithms/Java/trees/check-tree-traversal.java @@ -0,0 +1,137 @@ +/** Author : Suraj Kumar + * Github : https://github.com/skmodi649 + */ + + + +/** PROBLEM DESCRIPTION : + * Given Preorder, Inorder and Postorder traversals of some tree of size N. + * The task is to check if they are all of the same tree or not + */ + + + +/** ALGORITHM : + * The root element will be the first element of preorder. + * Search for root in the inorder array and store it’s index as idx. + * Use this idx to determine elements of left and right subtree in all three traversal arrays. + * Call function recursively for both left and right sub tree. + */ + + + + +import java.lang.*; +import java.util.*; + +class Tree{ + int val; + Tree left, right; + Tree(int val){ + this.val = val; + left = null; right = null; + } +} + +class Main{ + static int idx; + static boolean isPossible; + + public boolean checktree(int[] preorder, int[] inorder, int[] postorder, int N){ + idx = 0; + isPossible = true; + Map hmap = new HashMap<>(); + for(int i = 0; i < inorder.length; i++) hmap.put(inorder[i], i); + Tree root = buildTree(inorder, preorder, hmap, 0, N-1); + if(!isPossible) return false; + List post = new ArrayList<>(); + buildPost(root, post); + + return Arrays.equals(post.stream().mapToInt(i->i).toArray(), postorder); + } + + private static void buildPost(Tree root, List post){ + if(root == null) return; + buildPost(root.left, post); + buildPost(root.right, post); + post.add(root.val); + } + + private static Tree buildTree(int[] inorder, int[] preorder, Map hmap, int start, int end){ + if(start > end) return null; + if(!isPossible) return null; + int val = preorder[idx++]; + Tree root = new Tree(val); + if(!hmap.containsKey(val)){ + isPossible = false; + return null; + } + int pos = hmap.get(val); + if(pos < start || pos > end){ + isPossible = false; + return null; + } + root.left = buildTree(inorder, preorder, hmap, start, pos-1); + root.right = buildTree(inorder, preorder, hmap, pos+1, end); + return root; + } + + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the value of N : "); + int n = sc.nextInt(); + int[] preorder = new int[n]; + int[] inorder = new int[n]; + int[] postorder = new int[n]; + System.out.println("Enter the elements of preorder array : "); + for(int i=0; i