[leetcode]_根据二叉树的先序遍历(后序遍历) + 中序遍历 重建二叉树
题目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]_根据二叉树的先序遍历(后序遍历) + 中序遍历 重建二叉树的更多相关文章
- [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 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
- 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 ...
- PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)
前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个结点只能访问一次.要特别注意的是,二叉树的深度优先遍历比较特殊,可以细分为先序遍历.中序遍历.后序遍历.具体说明如下: 前序遍 ...
- 遍历二叉树 - 基于递归的DFS(前序,中序,后序)
上节中已经学会了如何构建一个二叉搜索数,这次来学习下树的打印-基于递归的DFS,那什么是DFS呢? 有个概念就行,而它又分为前序.中序.后序三种遍历方式,这个也是在面试中经常会被问到的,下面来具体学习 ...
- 【数据结构与算法】二叉树的 Morris 遍历(前序、中序、后序)
前置说明 不了解二叉树非递归遍历的可以看我之前的文章[数据结构与算法]二叉树模板及例题 Morris 遍历 概述 Morris 遍历是一种遍历二叉树的方式,并且时间复杂度O(N),额外空间复杂度O(1 ...
- 二叉树遍历(前序、中序、后序)-Java实现
一.前序遍历 访问顺序:先根节点,再左子树,最后右子树:上图的访问结果为:GDAFEMHZ. 1)递归实现 public void preOrderTraverse1(TreeNode root) { ...
- C语言实现链式二叉树静态创建,(先序遍历),(中序遍历),(后续遍历)
#include <stdio.h>#include <stdlib.h> struct BTNode{ char data ; struct BTNode * pLchild ...
- [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 ...
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
随机推荐
- JS 原生JS 判断滚动条滑动到底部
window.addEventListener("scroll", function(event) { var scrollTop = document.documentEleme ...
- SVN使用—工作模式及运行原理以及优缺点对比
一.SVN原理 (1)运行方式 svn服务器有2种运行方式:独立服务器和借助apache运行. 1.独立服务器访问 访问地址如:svn://svn.test.com/test 2.借助Apache等h ...
- const修饰的常量 不能被直接修改 但是可以通过指针进行间接修改
大家都知道如下代码中,被const限定的a是不可以被直接修改的 void main() { const int a = 3; a=1; } 在C++中const修饰的常量,不能被直接修改,但是可以通过 ...
- 20165114 《网络对抗技术》 Exp0 Kali安装与配置 Week1
目录: 一.kail的下载与安装 二.kali的网络设置 三.安装vmware-tools. 四.更新软件源. 五.共享文件夹 六.安装中文输入法 一.kail的下载与安装 VMware workst ...
- LeetCode——Find the Difference
LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...
- Mongodb笔记(三)user && aggregate && mapReduce
版本:mongodb3.4. User: mongodb使用验证登录:默认不开启,mongod中使用--auth开启: mongod -port=3000 --auth : 基本方法: db.cr ...
- Exception has been thrown by the target of an invocation 网站报错
最近因为要做一个启动器,在使用WPF做UI的时候,发现有错误如下: 错误 1 未知的生成错误"此实现不是 Windows 平台 FIPS 验证的加密算法的一部分. 行 8 位置 3.&quo ...
- eclipse indigo 安装 Eclipse Marketplace Client
打开 eclipse,help--Eclipse Marketplace Client就能找到 有的eclipse中没有这个功能就需手动添加Eclipse Marketplace Client. he ...
- python标准日志模块logging使用
python的标准库里的日志系统从Python2.3开始支持.只要import logging这个模块即可使用.如果你想开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件,只要这样使用: ...
- 0 与 “0" 与 '\0' 与 '0'相互之间的区别
1. '\0'和‘0’都是字符,对应的ASCII值分别是0和48. 2. 0表示一个数字.也可以表示ASCII值,对应字符'\0'. 3. “0”表示字符串,第一个字符是'0'.