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

2021-08-15 03:16:41 字數 408 閱讀 1642

輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列和中序遍歷序列,則重建二叉樹並返回。

class treenode 

}public

class s2

public treenode reconstruct(int pre,int ps,int pe,int

in,int

is,int ie)

treenode root=new treenode(pre[ps]); //每顆子樹的根節點肯定是pre子陣列的首元素,所以每次新建乙個子樹的根節點

for(int i=is;i<=ie;i++)

}return root;

}}

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

package com.study 根據二叉樹的前序遍歷和中序遍歷結果重建二叉樹 並輸出其頭節點。假設前序遍歷和中序遍歷結果中沒有重複數字 前序遍歷序列 中序遍歷序列 class treenode public class suanfa4 private static int arr2 public...

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

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

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

include stdafx.h include include include using namespace std 重建二叉樹 題目 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹.假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字.例如 輸入前序遍歷序列和中序遍歷序列,則重建出圖...