/**
-
注:此题使用ES6的Map,使得时间复杂度降为O(n)
-
关于Map和Set的使用要重点掌握!!!
-
我觉得思路很棒,继续理解!
*/
#剑指 Offer 32 - II. 从上到下打印二叉树 II
/**
-
Definition for a binary tree node.
-
public class TreeNode {
-
int val;
-
TreeNode left;
-
TreeNode right;
-
TreeNode(int x) { val = x; }
-
}
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
int inLen = inorder.length;
int postLen = postorder.length;
if(inLen != postLen){
throw new RuntimeException(“两个遍历输入错误”);
}
return createTree(inorder, 0, inLen - 1, postorder, 0, postLen - 1);
}
public TreeNode createTree(int[] inorder, int inLeft, int inRight,
int []postorder, int postLeft, int postRight){
if( inLeft > inRight || postLeft > postRight){
return null;
}
int pivot = postorder[postRight];
int pivotIndex = 0;
while(inorder[pivotIndex] != pivot){
pivotIndex++;
}
TreeNode root = new TreeNode(inorder[pivotIndex]);
// 这里的左右子树不要搞混了, root.left, 就是看 中序 和 后序的左子树, right 就是右边的
// 然后最好画图, 要不然不是很好理解
root.left = createTree(inorder, inLeft, pivotIndex - 1, postorder, postLeft, postRight - inRight + pivotIndex - 1);
root.right = createTree(inorder, pivotIndex + 1, inRight, postorder, postRight - inRight + pivotIndex, postRight -1);
return root;
}
}
这里分享一份由字节前端面试官整理的「2021大厂前端面试手册」,内容囊括Html、CSS、Javascript、Vue、HTTP、浏览器面试题、数据结构与算法。全部整理在下方文档中,共计111道
HTML
-
HTML5有哪些新特性?
-
Doctype作⽤? 严格模式与混杂模式如何区分?它们有何意义?
-
如何实现浏览器内多个标签页之间的通信?
-
⾏内元素有哪些?块级元素有哪些? 空(void)元素有那些?⾏内元 素和块级元素有什么区别?
-
简述⼀下src与href的区别?
-
cookies,sessionStorage,localStorage 的区别?
-
HTML5 的离线储存的使用和原理?
-
怎样处理 移动端 1px 被 渲染成 2px 问题?
-
iframe 的优缺点?
-
Canvas 和 SVG 图形的区别是什么?
JavaScript
-
问:0.1 + 0.2 === 0.3 嘛?为什么?
-
JS 数据类型
-
写代码:实现函数能够深度克隆基本类型
-
事件流
-
事件是如何实现的?
-
new 一个函数发生了什么
-
什么是作用域?
-
JS 隐式转换,显示转换
-
了解 this 嘛,bind,call,apply 具体指什么
-
手写 bind、apply、call
-
setTimeout(fn, 0)多久才执行,Event Loop
-
手写题:Promise 原理
-
说一下原型链和原型链的继承吧
-
数组能够调用的函数有那些?
-
PWA使用过吗?serviceWorker的使用原理是啥?
-
ES6 之前使用 prototype 实现继承
-
箭头函数和普通函数有啥区别?箭头函数能当构造函数吗?
-
事件循环机制 (Event Loop)