construct the binary tree.
一、题目
题目描述Given inorder and postorder traversal of a tree, construct the binary tree.Note:
You may assume that duplicates do not exist in the tree.
二、分析
中序:DBEAC
后序:DEBCA
通过结合图片和中序,后序序列可以很清楚地看到A是这棵树的根节点,并且有左右子树,左子树中有B,D,E,而右子树中只有一个C。通过观察后序序列可以发现根节点位于此序列的最右端,而根节点在中序序列中的左边是它的左子树,右边是它的右子树。根据这样的特性得出以下代码:
public class ConstructBinaryTree {public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}}/*** * @param inorder 中序序列* @param postorder 后序序列* @return*/public TreeNode buildTree(int[] inorder, int[] postorder) {if (inorder == null || postorder == null || inorder.length != postorder.length)return null;int starti = 0;int endi = inorder.length - 1;int startp = 0;int endp = postorder.length - 1;TreeNode root = build(inorder, postorder, starti, endi, startp, endp);return root;}public TreeNode build(int[] inorder, int[] postorder, int starti, int endi, int startp, int endp) {if (starti > endi || startp > endp)return null;TreeNode root = new TreeNode(postorder[endp]);int j = 0;for (int i = starti; i <= endi; i++) {if (root.val == inorder[i])j = i;//记录root节点在中序遍历中的位置}int len = j - starti;//左的长度//递归分别对root节点的左子树和右子树进行buildroot.left = build(inorder, postorder, starti, j - 1, startp, startp + len - 1);root.right = build(inorder, postorder, j + 1, endi, startp + len, endp - 1);return root;}}
We should all be responsible for our own actions
construct the binary tree.相关推荐
- Codeforces Round #624 (Div. 3) E. Construct the Binary Tree 思维 + 构造
传送门 文章目录 题意: 思路: 题意: 给你n,dn,dn,d,让你构造有nnn个点的二叉树,他们每个节点深度和为ddd. n,d≤3000n,d\le 3000n,d≤3000. 思路: 先考虑不 ...
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- Construct Maximum Binary Tree
构建一颗「最大树」. 注意consruct的时候最后的return root; 我参考了serialize and deserialize binary tree的build tree 的过程. 这周 ...
- 【CF1311E】Construct the Binary Tree【增量构造】【复杂度证明】
题意:给定nnn和ddd,构造或判断无法构造一棵二叉树使得所有点的深度(定义为到根距离)之和为ddd. n,d≤5000n,d\leq 5000n,d≤5000 显然可以算出有解的ddd的下界和上界, ...
- Given inorder and postorder traversal of a tree, construct the binary tree
输入某二叉树的中序遍历和后序遍历的结果,请重建出该二叉树. 例如如下二叉树 中序:8 4 2 5 1 6 9 3 7 后序:8 4 5 2 9 6 7 3 1 后序的最后一个节点为根节点,在中序遍历中 ...
- leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)...
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal-前序中序遍历构造二叉树-Python和Java递归解法
题目地址:Construct Binary Tree from Preorder and Inorder Traversal - LeetCode Given preorder and inorder ...
- 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 ...
- LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
最新文章
- OpenCV 【十七】离散傅立叶变换
- uboot学习之BL3的流程
- 将单向链表按某值分成左边小、中间相等、右边大的形式
- 登录mysql报错2059_navicat连接mysql报错2059的解决方法
- php 内部异步执行顺序,event_loop中不同异步操作的执行顺序
- dump分析工具_Java应用CPU过高,如何排查?参考解决思路和常用工具总结
- yapi 插件_精神多了,当Swagger遇上YApi,瞬间高大上了!
- redis 集群环境部署
- Hexo 入门指南(六) - sitemap、rss 和部署
- 运筹学动态规划逆序解法_运筹学第七章 动态规划讲解.ppt
- smail语法 详解
- 告别微服务:究竟是千军易得还是一将难求
- 守望先锋的蜂窝盾制作
- html5源码笔记【爱创课堂专业前端培训】
- 石墨文档软件服务器,软件技巧 篇十三:工作学习好帮手,在线文档使用体验之石墨文档...
- java廖雪峰_廖雪峰Java教程学习笔记(一)——Java程序基础
- python学习笔记05——字符串
- 点餐系统---------软件工程课程设计
- python 界面一
- redis常用命令个人总结