Problem: Check if two n-ary tree are mirror to each other. Two trees are mirror if : the key/data value of the root are same of the two trees left sub tree is same as right sub tree of another tree right sub tree is same as left sub tree of another tree The problem
Category: Tree
Tree construction a Binary Tree from Postorder and Inorder traversal [Amazon, Adobe]
Problem: Provided Inorder and PostOrder of a tree. Construct the tree and print it in PreOrder traversal. #include <stdio.h> #include <iostream> #include <string.h> #include<stdlib.h> #include <string> using namespace std; struct Node{ int data; Node *left; Node *right; }; int search(int in[],int in_stIndex,int in_endIndex,int data) { for(int i=in_stIndex;i<=in_endIndex;i++) { if(in[i]==data) return i; } } void pintPreOrder(Node*
Tree construction from Inorder & Preorder [ Microsoft ]
Problem: Provided Inorder and PreOrder of a tree. Construct the tree and print it in PostOrder traversal. #include <stdio.h> #include <iostream> #include <string.h> #include<stdlib.h> #include <string> using namespace std; int preOrderIdx=0; struct Node{ int data; Node *left; Node *rit; }; int search(int in[],int in_stIndex,int in_endIndex,int data) { for(int i=in_stIndex;i<=in_endIndex;i++) { if(in[i]==data) return i; } }
Continue Reading “Tree construction from Inorder & Preorder [ Microsoft ]”
12347
#include <iostream> #include <vector> #include <algorithm> #include <fstream> using namespace std; int pre[10001]; int in[10001]; ofstream wrt; int search(int _in[],int a,int len) { for(int i=0;i<len;i++) { if(_in[i]==a) return i; } return -1; } void printPostOrder(int _in[],int _pre[],int n) { int root=search(_in,_pre[0],n); if(root!=0) printPostOrder(_in,_pre+1,root); if(root!=n-1) printPostOrder(_in+root+1,_pre+root+1,n-root-1); cout<<_pre[0]<<"\n"; //wrt<<_pre[0]<<"\n"; } int main() { int i=0; int a;
536
Tree Recursion #include <iostream> #include <stdio.h> #include <string.h> #include <string> using namespace std; // A utility function to search x in arr[] of size n int search(char arr[], char x, int n) { for (int i = 0; i < n; i++) if (arr[i] == x) return i; return -1; } // Prints postorder traversal