Skip to content

Create a binary tree from inorder and preorder traversal given in array form. #2707

@kumanoit

Description

@kumanoit

Create a binary tree from inorder and preorder traversal.
Given inorder and preorder traversal notation of a binary tree. Create the tree from it

Describe the solution you'd like

  1. The first element of preorder array will be the root of binary tree. So create root from it
  2. In inorder traversal all the elements towards left of an index-i forms left subtree for tree rooted at 'i' and right part of array forms the right subtree.
  3. Hence, find the index in inorder representation for the first element of preorder array. Say it occurs at i-th index.
  4. Traverse those many elements in preorder array and use that part of preorder array and inorder[0...i-1] to create left subtree
  5. Similary use inorder[i+1...] and rest of elements in preorder array to create right subtree.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions