Tag : binary-tree

#include <iostream> #include<bits/stdc++.h> using namespace std; struct node { int data; node* r; node* l; node(int data):data(data){}; }; void add(node*&root,vector<int>value,vector<char>direction) { assert(value.size()==direction.size()); node*curr=root; for(int i=0;i<(int)value.size();i++) { if(direction[i]==’R’) { if(curr->r) assert(curr->r->data==value[i]); else { node*n=new node(value[i]); curr->r=n; curr=curr->r; } } else if(direction[i]==’L’) { if(curr->l) assert(curr->l->data==value[i]); else { node*n=new node(value[i]); curr->l=n; curr=curr->l; } } } } void print_inorder(node*curr) ..

Read more

Given a binary tree is a perfect binary tree and the nodes are represented in a numeric way which gives consecutive numbers to each depth from left to right. Can we derive the in order and pre order orders of the trees for all depth? Numeric representation is given as an example for a tree of depth 3. In order is [4, 2, 5, 1, 6, 3, 7] and the pre order is [1, 2, 4, 5, 3, 6, 7]. My question is can we automatically derive this lists by given only depth with a mathematical approach?
Perfect Binar..

Read more