Samsung twisted binary tree question Pre-Order traversal
Anonymous User
569

*I was asked to reconstruct a binary tree from pre-order traversal with markers..

for eg:***

												H
											/      \
										B           C
									/     \              \
								F           E             D
								           /                 \
										 A                    G
										                       /
															I
				```
															
***	for above binary tree pre-order traversal given as H   B   F   NULL   NULL   E   A   NULL   NULL   NULL   C   NULL   D   NULL   G   I   NULL   NULL   NULL.***
	
	Solution:
	```class Solution {
			public:
					TreeNode* reconstructBinaryTree(vector<int> preorder){
						return helper(preorder,0);
					}
			TreeNode* helper(vector<int>& preorder,int *traverse_index){
				int& index=*traverse_index;
				int* data=preorder[index];
				index++;
				if(data==NULL) return NULL;
				TreeNode* root=new TreeNode(data);
				root->left=helper(preorder,traverse_index);
				root->right=helper(preorder,traverse_index);
				return root;
			}
	};
Comments (1)