Posts

Showing posts with the label tree

Check if a binary tree is subtree of another binary tree

Given two binary trees, check if the first tree is subtree of the second one. Code: package trees; public class N2011_subTree {         Node root1, root2;     static class Node{         int data;         Node left, right;         Node(int data){             this.data = data;             left = right = null;         }     }     static boolean isIdentical(Node n1, Node n2){         if(n1 == null && n2 == null){             return true;         }         if(n1 != null && n2 != null){             return (n1.data =...

Print Postorder traversal from given Inorder and Preorder traversals

Input: Inorder traversal in[] = {4, 2, 5, 1, 3, 6} Preorder traversal pre[] = {1, 2, 4, 5, 3, 6} Output: Postorder traversal is {4, 5, 2, 6, 3, 1} Code: package trees; public class N2003_PrintPostOrer {     class Node{         int data;         Node left , right;         Node(int data){             this.data = data;             left = right = null;         }     }     static int findIndex(int element, int[] a, int start, int end){         int i= -1;         for(i =start; i<= end; i++){             if(element == a[i]){                 return i;     ...