Data structure learning thinking: the number of different binary trees of the preorder sequence a, b, c, d

408, [2015 Unified Exam Questions] The number of different binary trees of the preorder sequence a, b, c, d is ()

A.13                  B.14                  C.15                  D.16


There is a clever method, that is, a, b, c, d is the input stack sequence, and the sequence corresponding to the output stack is the number of different binary trees, which arouses the author's thinking, why can it be answered in this way.

To clarify this problem, we must understand the relationship between the pre-order traversal and in-order traversal of the binary tree. A certain pre-order traversal and in-order traversal can determine a binary tree. This is beyond doubt, and because a, b, c , d is the binary tree of the input stack sequence, each one must correspond to a stack sequence, and each binary tree must correspond to an in-order sequence, so it can be deduced that each in-order sequence must correspond to a pop-out sequence, that is, a A definite binary tree.

This description is a bit abstract, and then explained in the form of code.

The first is a non-recursive algorithm for preorder traversal of a binary tree:

viod PreOrder2(BiTree T){
    InitStack(S); BiTree p=T;        //初始化栈S;p是遍历指针
    while(p||IsEmpty(S)){            //栈不空或者p不空时循环
        if(p){                       //一路向左
            visit(p);Push(S,p);      //访问当前节点,并入栈
            p=p->lchild;             //左孩子不空,一直向左走
        }
        else{                        //出栈,并转向出栈节点的右子树
            Pop(S,p);                //栈顶元素出栈
            p=p->rchild;             //向右子树走,p赋值为当前节点的右孩子
        }                            //返回while循环继续进入if-else语句
    }
}

We can see that the visit(p)  statement and  the Push(S,p)  statement appear in pairs, which means that the sequence of the preorder traversal is the order of the stack

Next is a non-recursive algorithm for inorder traversal of a binary tree:

viod InOrder2(BiTree T){
    InitStack(S); BiTree p=T;        //初始化栈S;p是遍历指针
    while(p||IsEmpty(S)){            //栈不空或者p不空时循环
        if(p){                       //一路向左
            Push(S,p);               //访问当前节点,并入栈
            p=p->lchild;             //左孩子不空,一直向左走
        }
        else{                        //出栈,并转向出栈节点的右子树
            Pop(S,p);visit(p);       //栈顶元素出栈
            p=p->rchild;             //向右子树走,p赋值为当前节点的右孩子
        }                            //返回while循环继续进入if-else语句
    }
}

We can see that the visit(p)  statement and  the Pop(S,p)  statement appear in pairs, which means that the sequence of in-order traversal is the order of popping

Therefore, we can know from this that a binary tree determines the number of tree shapes corresponding to the binary tree of the pre-order sequence, which can be converted into this sequence as the stacking sequence to solve the number of different popping sequences. At this time, use Carter Lanshu can quickly find the answer to the question

\huge f(n)=C_{2 n}^{n}-C_{2 n}^{n-1}=\frac{1}{n+1}C_{2 n}^{n}

Guess you like

Origin blog.csdn.net/qq_21891843/article/details/123874209