题目1:Construct Binary Tree from Preorder and Inorder Traversal

给定一棵二叉树的先序遍历和中序遍历,求重建二叉树。

思路:

  1、先序遍历的第一个节点一定是根节点。

  2、在中序遍历中找到该根节点的位置(由中序遍历性质,决定其在中部),将中序遍历数组划分为两段,根节点左端的为左子树部分,相反右端的为右子树部分。

  3、由上述左、右子树的长度,决定在先序遍历中,左右子树对应数组的位置。

  4、递归 1 ~ 3步,直到子数组长度为1,结束递归。

代码:

 public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder == null || inorder == null || preorder.length == 0 || inorder.length == 0) return null; TreeNode root = buildTreeRecursive(preorder , 0 , preorder.length - 1 , inorder , 0 , inorder.length - 1);
return root;
} public TreeNode buildTreeRecursive(int[] preOrder , int preStart , int preEnd , int[] inOrder , int inStart , int inEnd){
int value = preOrder[preStart];
//1、先序遍历中的第一个节点一定是根节点。
TreeNode node = new TreeNode(value); //结束条件:如果长度为1,则返回该节点。
if(preStart == preEnd) return node; //2、在中序遍历中查找该节点的位置。
int index = 0;
for(index = inStart ; index <= inEnd && inOrder[index] != value; ){ index++; } //3、确定左右子树对应数组的位置后,递归调用。
int leftLen = index - inStart;
int rightLen = inEnd - index; if(leftLen > 0){
node.left = buildTreeRecursive(preOrder , preStart + 1 , preStart + leftLen , inOrder , inStart , index - 1);
}
if(rightLen > 0){
node.right = buildTreeRecursive(preOrder , preEnd - rightLen + 1 , preEnd , inOrder , index + 1 , inEnd);
} return node;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

题目二:Construct Binary Tree from Inorder and Postorder Traversal

给定一棵二叉树的中序遍历和后序遍历,重建二叉树。其思路与题目一完全一样。只是从postOrder确定根节点的位置,然后同样放到inOrder中去划分左右子树。

代码:

    public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder == null || postorder == null || inorder.length == 0 || postorder.length == 0) return null; TreeNode root = buildTreeRecursive(inorder , 0 , inorder.length - 1 , postorder , 0 , postorder.length - 1);
return root;
} public TreeNode buildTreeRecursive(int[] inOrder , int inStart , int inEnd ,
                         int[] postOrder , int postStart , int postEnd){ int value = postOrder[postEnd];
TreeNode node = new TreeNode(value);
if(postStart == postEnd) return node; // only one node int index = -1;
for(index = inStart ; index <= inEnd && inOrder[index] != value ; index++); // find in inOrder int leftLen = index - inStart;
if(leftLen > 0){
node.left = buildTreeRecursive(inOrder , inStart , index - 1 , postOrder , postStart , postStart + leftLen - 1);
}
int rightLen = inEnd - index;
if(rightLen > 0){
node.right = buildTreeRecursive(inOrder , index + 1 , inEnd , postOrder , postEnd - rightLen , postEnd - 1);
} return node; }

这两道题思路理清了,到也很流畅。:)

[leetcode]_根据二叉树的先序遍历(后序遍历) + 中序遍历 重建二叉树的更多相关文章

  1. [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  2. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  3. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)

    前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个结点只能访问一次.要特别注意的是,二叉树的深度优先遍历比较特殊,可以细分为先序遍历.中序遍历.后序遍历.具体说明如下: 前序遍 ...

  5. 遍历二叉树 - 基于递归的DFS(前序,中序,后序)

    上节中已经学会了如何构建一个二叉搜索数,这次来学习下树的打印-基于递归的DFS,那什么是DFS呢? 有个概念就行,而它又分为前序.中序.后序三种遍历方式,这个也是在面试中经常会被问到的,下面来具体学习 ...

  6. 【数据结构与算法】二叉树的 Morris 遍历(前序、中序、后序)

    前置说明 不了解二叉树非递归遍历的可以看我之前的文章[数据结构与算法]二叉树模板及例题 Morris 遍历 概述 Morris 遍历是一种遍历二叉树的方式,并且时间复杂度O(N),额外空间复杂度O(1 ...

  7. 二叉树遍历(前序、中序、后序)-Java实现

    一.前序遍历 访问顺序:先根节点,再左子树,最后右子树:上图的访问结果为:GDAFEMHZ. 1)递归实现 public void preOrderTraverse1(TreeNode root) { ...

  8. C语言实现链式二叉树静态创建,(先序遍历),(中序遍历),(后续遍历)

    #include <stdio.h>#include <stdlib.h> struct BTNode{ char data ; struct BTNode * pLchild ...

  9. [LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...

  10. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

随机推荐

  1. mysql中int(M) tinyint(M)中M的作用

    原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int(11)是指11个字节,int(10)就是10个字节.我错了. http://zhidao.baidu.com/li ...

  2. oracle 11 g数据库卸载(方法二)

    1.开始->设置->控制面板->管理工具->服务 停止所有 Oracle服务. 2.开始->程序->Oracle - OraHome81->Oracle In ...

  3. 2018-2019-2 20165114《网络对抗技术》Exp4 恶意代码分析

    Exp4 恶意代码分析 目录 一.实验目标 (1)监控你自己系统的运行状态,看有没有可疑的程序在运行. (2)分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sys ...

  4. Mac & Xcode 技巧

    1.Xcode 删除过期的Provisioning Profile文件 解决:Xcode 中所有的Provisioning Profile文件路径,都在 ~/Library/MobileDevice/ ...

  5. Windows7中pagefil.sys和Hiberfil.sys文件删除与转移

    第一步.在开始的功能表的搜索栏里输入 cmd,然后在搜索结果中的 cmd 按下右键,点选[以系统管理员身分运行] 第二步.在命令提示符窗口里输入下面命令然后按下Enter: powercfg –h o ...

  6. 怎么用Python提取域名中的主域名

    从一个域名里面提取主域名,初想起来,貌似很简单,不就是数点[.]的个数吗?取最后一个点前后的字符串,那 abc.txt 是域名吗?那再加个验证,加上国家码,.com,.cn,.org结尾的才算,那这个 ...

  7. python 之Tornado

    一.Tomado Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webap ...

  8. Python连接MongoDB操作

    1.安装PyMongo 注意:请勿安装“bson”软件包. PyMongo配有自己的bson包; 执行“pip install bson”或“easy_install bson”则会安装与PyMong ...

  9. NativeWindow_01

    1. “{$O-}”关闭优化 “{$O-}”打开优化 2. unit NativeWindow; interface uses Windows, Messages, SysUtils; procedu ...

  10. Springboot- Spring缓存抽象学习笔记

    Spring缓存作用准备: 1.准备数据(准备一个有数据的库和表/导入数据库文件,准备好表和表里面的数据) 2.创建javaBean封装数据 3.整合MyBatis操作数据库( 这里用MyBatis) ...