Complete binary tree pre-middle-post-order->layer-order

Topic description

A binary tree, if the number of nodes in each layer reaches the maximum value, then this binary tree is a perfect binary tree. For a binary tree of depth D with N nodes, if its nodes correspond to the first N nodes of the level-order traversal of a perfect binary tree of the same depth, such a tree is a complete binary tree.

Given the post-order traversal of a complete binary tree, please give the result of the level-order traversal of the tree.
As in the question, when the title gives the pre-, middle- and post-order traversal of the binary tree, it can be directly converted to level-order traversal

void dfs(int i) {
    
    
    if (i > n) return;
    
    //scanf("%d", &tree[i]); // 前->层
    dfs(i*2);
    //scanf("%d", &tree[u]); // 中->层
    dfs(i*2+1);
    scanf("%d", &tree[i]); // 后->层
}

ReferencesLevel
-order traversal of complete binary treeLuo_LA

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324168811&siteId=291194637