(Help) Tree to Array Conversion

sss

How to represent the tree here in the form of an Array?

@everule1, can you help me with this question?

@ay2306?

If it was a complete binary tree then you could represent in array. You can use matrix or 2D vector for representation

1 Like

Okay@sebastian what is time complexity of a skewed binary tree?

@sebastian

If tree of limited height and is binary tree, it can represented in array like in segment tree or heaps.
Arr[i] = value
Left child of left = 2*i+1
and right child as right = 2*i+2

So arr = [-1,9,8,11,-1,23,12,34,-1,10]

I think this is a valid answer. Sorry if I missed something.

1 Like

Are you talking about modified dfs order traversal? It’s somewhere in this blog.

2 Likes

Thanks, @ay2306, @everule1, and @sebastian for your efforts and kind help.
I like this community for its helping nature.
Please keep doing the good work and thanks a lot for clearing my doubts.

Regards,
Pen

2 Likes

I guess it should be O(n)

1 Like

You can apply tree flattening algorithm.

1 Like