国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

重拾算法之劍指Offier——重建二叉樹

2019-11-10 22:01:34
字體:
來源:轉載
供稿:網友

劍指Offier——重建二叉樹

題目描述 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重復的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹并返回。

public class Solution { public TreeNode reConstructBinaryTree(int [] PRe,int [] in) { return BuildTree(pre, in, 0, pre.length-1, 0, in.length-1); } public TreeNode BuildTree(int [] pre, int [] in, int preSt, int preEn, int inSt, int inEn){ if(preSt > preEn || inSt > inEn){ return null; } TreeNode treeNode = new TreeNode(pre[preSt]); for (int i=inSt; i<=inEn; i++){ if(pre[preSt] == in[i]){ treeNode.left = BuildTree(pre, in, preSt+1, preEn, inSt, i-1); treeNode.right = BuildTree(pre, in, preSt+1+i-inSt,preEn,i+1, inEn); } } return treeNode; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 分宜县| 九龙城区| 余姚市| 绩溪县| 阳高县| 兴仁县| 西畴县| 保定市| 嘉定区| 叶城县| 铜山县| 高邑县| 金坛市| 昌黎县| 邹平县| 崇明县| 东乌珠穆沁旗| 含山县| 微山县| 锡林浩特市| 安吉县| 霍城县| 电白县| 元阳县| 西林县| 中超| 金沙县| 民乐县| 龙岩市| 兴宁市| 台江县| 虹口区| 建始县| 清新县| 大港区| 西畴县| 咸丰县| 宾川县| 宣化县| 秭归县| 当涂县|