Exemplo n.º 1
0
int main(){
  node *root;
  createTree(&root);
  printf("Recursive Pre order traversal\n");
  preorderTraversalRecursive(root);
  printf("\nNonRecursive Pre order traversal\n");
  preorderTraversalNonRecursive(root);
  printf("Recursie Inorder Traversal\n");
  inorderTraversalRecursive(root);
  printf("\nNonRecursive In order traversal\n");
  inorderTraversalNonRecursive(root);
  printf("\nRecursice Postorder traversal\n");
  postorderTraversalRecursive(root);
  printf("\n");//NonRecursive Postorder traversal\n");
  //postorderTraversalNonRecursive(root);
  createMirror(root);
  printf("\nMirrored Tree:\n");
  printf("Recursive Pre order traversal\n");
  preorderTraversalRecursive(root);
  printf("\nNonRecursive Pre order traversal\n");
  preorderTraversalNonRecursive(root);
  printf("Recursie Inorder Traversal\n");
  inorderTraversalRecursive(root);
  printf("\nNonRecursive In order traversal\n");
  inorderTraversalNonRecursive(root);
  printf("\nRecursice Postorder traversal\n");
  postorderTraversalRecursive(root);
  printf("\n");//NonRecursive Postorder traversal\n");
  printf("\nVertical Traversal: \n");
  verticalTraversal(root);
  return 0;
}
Exemplo n.º 2
0
/*
* Postorder Traversal:
* termination:
* if: node is NULL
* 
* call Recursively for left child
* call Recusively for right child
* print the node
*/
void postorderTraversalRecursive(node *ptr){
  if(ptr == NULL)
    return;
  postorderTraversalRecursive(ptr->left);
  postorderTraversalRecursive(ptr->right);
  printf("%d ",ptr->data);
}
	void postorderTraversalRecursive(TreeNode *cur, vector<int> &q) {
		if(cur == NULL)
			return;

		postorderTraversalRecursive(cur->left, q);
		postorderTraversalRecursive(cur->right, q);
		q.push_back(cur->val);
	}
    vector<int> postorderTraversal(TreeNode *root) {
		vector<int> ans;
		postorderTraversalRecursive(root, ans);
		return ans;
	}