[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 ...
随机推荐
- leetcode每日一题——两数之和
题目: 两数之和 难度: 简单 描述: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 解法: class Solutio ...
- Bootstrap总结二
参考我的博客:http://www.isedwardtang.com/2017/09/01/bootstrap-primer-2/
- LeetCode: Max Consecutive Ones
这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...
- jmeter之报告和分析
转载:http://www.cnblogs.com/miaomiaokaixin/p/6118081.html jmeter -n -t 脚本名字.jmx -l xxx.jtl -e -o 指定目录( ...
- 双摄像头测距的OpenCV实现
http://blog.csdn.net/scyscyao/article/details/5562024 版权声明:本文为博主原创文章,未经博主允许不得转载. 虽然最近注意力已经不可遏制地被神经科学 ...
- gh-ost测试
gh-ost测试 1.不支持没有主键或者唯一索引的表 2018-08-24 09:53:33 FATAL No PRIMARY nor UNIQUE key found in table! Baili ...
- 20145109 《Java程序设计》第六周学习总结
Chapter 10 I/O 10.1 InputStream & OutputStream a new 'try' edition: try (InputStream input = src ...
- Oracle数据库的数据导入导出
--备份数据库--数据库系统用户账号system/adminuser --查看oracle数据库的用户select * from all_users;--查看oracle数据库的版本号select * ...
- tar 解压命令学习与总结
tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...
- 初入spring boot(五 )websocket
一.广播式 广播式即服务端有消息时,会将消息发送给所有连接了当前endpoint的浏览器 1.配置websocket,需要在配置类上使用@EnableWebSocketMessageBroker开启w ...