BBranchSpire
Product

Product features · Traversal-based tree reconstruction

01

Review traversal fixtures at the parser boundary, then inspect reconstructed child positions before a downstream consumer receives the nested tree.

01

Connection

Base URL, credentials and a reachable server.

02

Selection

A model available through the configured endpoint.

03

Capabilities

Context limits and tool support are determined by the model and server.

Traversal-based tree reconstruction

● ● ●BranchSpire●
A concrete starting point · Traversal-based tree reconstruction ×
def rebuild_tree(preorder, inorder):
    for row in (preorder,inorder):
        if type(row) not in (list,tuple) or len(row) > 100 or any(type(v) is not int for v in row):
            raise ValueError("at most 100 integer nodes")
    if len(set(preorder)) != len(preorder) or len(set(inorder)) != len(inorder) or set(preorder) != set(inorder):
        raise ValueError("same distinct node labels")
    positions, cursor = {v:i for i,v in enumerate(inorder)}, 0
    def visit(lo,hi):
        nonlocal cursor
        if lo == hi: return None
        value = preorder[cursor]; cursor += 1
        at = positions[value]
        if not lo <= at < hi: raise ValueError("inconsistent traversals")
        left = visit(lo,at)
        right = visit(at+1,hi)
        return value,left,right
    return visit(0,len(inorder))
✳Rebuild a binary tree from preorder and inorder lists of at most 100 distinct integer labels.
✓ ChangesReview before applying ↗
Illustrative workspace
AI coding workspace

Your next change starts here.

Get the Windows app ↗