重構二叉樹 根據前序和中序

2021-06-02 10:16:23 字數 754 閱讀 1604

// 重構二叉樹.cpp : 定義控制台應用程式的入口點。

#include "stdafx.h"

#include "iostream"

#include "stdlib.h"

#include "stdio.h"

#include "string.h"

using namespace std;

int i,j;

struct binarynode

;binarynode *repair(char *prestr,char *orderstr,int n)

void printnode(binarynode *t,char *str,char *result)

printnode(t->left,str,result);

printnode(t->right,str,result); }}

int main()

,prestr[1000]=;

int num;

cin>>num;

while(num--)

;cin>>prestr;

cin>>orderstr;

cin>>m;

for(int k=0;k>str[k];

root=repair(prestr,orderstr,strlen(prestr));

printnode(root,str,result);

for(n=0;n<2*(m-1);n=n+2)

cout<

07 二叉樹 根據前序和中序遍歷重建二叉樹

問題 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。分析 1 先根據前序遍歷的第乙個節點root找到分界點i 2 將前序遍歷陣列和中序遍歷陣列按照分界點進行劃分 3 遞迴 實現 public treenode buildtree int...

重建二叉樹 根據前序遍歷和中序遍歷構建二叉樹

輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列和中序遍歷序列,則重建二叉樹並返回。思路 1.前序遍歷的第乙個節點一定是根結點。前序遍歷的乙個節點要麼是相鄰前乙個節點的左子樹 右子樹或者更靠近前面節點的右子樹。如何確...

重建二叉樹,根據前序中序遍歷構建二叉樹

輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列和中序遍歷序列,則重建二叉樹並返回。前序遍歷 根左右 中序遍歷 左根右 根據前序遍歷我們可以知道根節點,根據中序遍歷我們可以知道根節點的左右子樹結點有哪些。如 前序 中...