[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 ...
随机推荐
- Windows 下c获取文件目录
由于要插数据库,构建sql语句,需要文件名,在网上找了半天,无奈都是Linux下的专用函数,伤心,,还有那个下载URL ,还木搞好,就要走啦,心焦哇 #include<iostream> ...
- CSS3自定义发光radiobox单选框
在线演示 本地下载
- java没有指针
先说结论:java没有指针,它使用对象引用来替代指针 备注:c/c++的引用和java的引用完全不是一个东西 c/c++的引用是同一块内存的不同名字 java的引用指向一个对象,引用本身也占用了内存 ...
- SQLMAP 使用手册
当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数 2.判断可以用那种SQL注入技术来注入 3.识别出哪种数据库 4.根据用户选择,读取哪些数据 sqlmap支持五种不同的注入模式: ...
- mongoose@4.5.2的eachAsync bug
自称踩坑大王,幸好没有地雷,哈哈哈哈哈哈,今天用了mongoose的 eachAsync() 方法,没想到,会出现 Trace: [RangeError: Maximum call stack siz ...
- UML学习-1 UML 简介
UML 是什么 Unified Modeling Language(UML)又称统一建模语言或标准建模语言,是始于 1997 年一个 OMG 标准,它是一个支持模型化和软件系统开发的图形化语言,为软件 ...
- PasswordHasher 算法
public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string provid ...
- ES6 Babel 简单使用
ECMAScript 是 JS 的语言标准.而 ES6 是新的 JS 语法标准. PS:严格来说,ECMAScript 还包括其他很多语言的语言标准. ECMAScript 发展历史 1995年:EC ...
- nagios无法载入静态资源
使用nginx+nagios无法载入静态资源,看了下url中增加了一个/nagios 查看是/usr/local/nagios/etc/cgi.conf中url_html_path=/nagios 将 ...
- HTTP Status 500 - java.lang.NoClassDefFoundError: JspTagException
HTTP Status 500 - java.lang.NoClassDefFoundError: JspTagException cause java.lang.NoClassDefFoundEr ...