leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal | 106. 从中序后序遍历序列构造二叉树(Java)
题目
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
题解
待优化:可以用 map 存一下每一个前序遍历元素对应的下标,这样更快一些。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {return process(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);}public TreeNode process(int[] inorder, int[] postorder, int L1, int R1, int L2, int R2) {if (L1 > R1) return null;TreeNode root = new TreeNode(postorder[R2]);for (int M1 = L1, n = 0; M1 <= R1; M1++, n++) {if (inorder[M1] == postorder[R2]) {root.left = process(inorder, postorder, L1, M1 - 1, L2, L2 + n - 1);root.right = process(inorder, postorder, M1 + 1, R1, L2 + n, R2 - 1);return root;}}return root;}
}
leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal | 106. 从中序后序遍历序列构造二叉树(Java)相关推荐
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- 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 - 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 tha ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
最新文章
- oracle schema与mysql_Oracle数据库之Oracle 11g R2 用户与模式(schema)
- java jdk动态代理学习记录
- Prometheus + Granafa 构建高大上的MySQL监控平台
- Python递归算法以及命名空间
- JavaScript window.getComputedStyle()
- lombok 中的@Data注解
- java gz文件解压_java – 批量解压缩.gz文件
- 最新70佳单页网站设计案例欣赏(上篇)
- Java多线程同步机制
- 苹果或推出Windows版Safari浏览器;微软正“取下”Chromium版Edge浏览器的Beta标签;亚马逊申请新专利……...
- 可视化:动态气泡图,拿走不谢!
- python开发软件的实例-这可能是最好玩的python GUI入门实例!
- 2018年python工作好找吗-2018年 Python面试必看的10个问题及答案
- 伪静态在webconfig中配置
- Windows2000系统下Apache2和PHP4安装终级宝典
- 毕设题目:Matlab语音去噪
- 《越狱》的中国隐秘流行
- Vs2010中文版 使用 .net 3.5 时,智能提示英文变中文
- 2020年中华人民共和国行政区划代码
- python re模块详解_re模块详解