根據二叉樹的先序遍歷和中序遍歷建立二叉樹

2021-04-02 22:01:59 字數 954 閱讀 6097

根據二叉樹的先序遍歷和中序遍歷建立二叉樹

rebacktree 根據二叉樹的先序遍歷和中序遍歷建立二叉樹。

rebacktree1 根據二叉樹的後序序遍歷和中序遍歷建立二叉樹。

#include "stdio.h"

#include "stdlib.h"

typedef struct node

node,*nodep;

void midorder(nodep p);

nodep rebacktree (nodep t,char a,char b,int al,int ah,int bl,int bh);

nodep rebacktree1 (nodep t,char a,char b,int al,int ah,int bl,int bh);

main()

void midorder(nodep p)

}nodep rebacktree (nodep t,char a,char b,int al,int ah,int bl,int bh)

t->lchild=rebacktree(t->lchild,a,b,al+1,al+j-bl,bl,j-1);

t->rchild=rebacktree(t->rchild,a,b,al+j-bl+1,ah,j+1,bh);

return t;

}return null;

}nodep rebacktree1 (nodep t,char a,char b,int al,int ah,int bl,int bh)

t->lchild=rebacktree1(t->lchild,a,b,al,al+j-bl-1,bl,j-1);

t->rchild=rebacktree1(t->rchild,a,b,al+j-bl,ah-1,j+1,bh);

return t;

}return null;

}

根據先序遍歷和中序遍歷建立二叉樹

先序遍歷的順序是根左右,中序遍歷的順序是左根右。根據這一特性,先序遍歷的第一個元素肯定是根節點。所以我們只要在中序遍歷中找到該根節點的值,根節點以左就是它的左子樹,根節點以右就是它的右子樹,然後就可以遞迴的方式建立二叉樹 假設現在有一顆二叉樹如下 先序序列為 18,14,7,3,11,22,35,2...

根據二叉樹的先序遍歷和中序遍歷重建二叉樹

輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列和中序遍歷序列,則重建二叉樹並返回。public class solution int in treenode root reconstructbinarytree p...

根據二叉樹的先序和中序遍歷輸出層序遍歷

寫一下我的一個思路,coding就不貼了。如何分割先序和後序的方法可以看我輸出後序遍歷那篇部落格,這裡只談談分割後怎麼遞迴能得到層次遍歷的方法。當然方法有很多,我這裡提供一種暴力一點,通用一點的方法。我給這種方法起了個名字叫標籤繫結法。所謂標籤繫結,就是我在把元素新增到result列表的時候,不是直...

二叉樹先序遍歷 中序遍歷 後序遍歷

輸入二叉樹的先序遍歷序列和中序遍歷序列,輸出該二叉樹的後序遍歷序列。非建二叉樹版本 include includeusing namespace std string preord,inord void rebuild int preleft,int preright,int inleft,int ...

二叉樹 先序遍歷 中序遍歷 後續遍歷

package com.example.ljia.structure.tree import lombok.data author samlai description 遞迴 二叉樹 先序遍歷 中序遍歷 後續遍歷 先序遍歷 根 左 右 中序遍歷 左 根 右 後序遍歷 左 右 根 發現規律 這裡的順序...