面试时间:2021.8.16
自我介绍
聊之前项目(使用的框架,遇到的问题,如何解决,有啥看法感悟)(感觉面试官自己也不知道问啥,所以这里就聊了足足半小时…)
Vue和React的区别你有什么看法
编程题
//结构定义
function TreeNode(v) {
this.val = v;
this.left = this.right = null;
}
//设置根节点
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);
level(root);
// 希望输出
// 7 6 5 4
// 3 2
// 1
//实现一个level函数
//思路:先 层序遍历,后 倒序输出
function level(root){
if(root == null) return;
let result = [];
let queue = [root];
while(queue.length > 0){
//当前层次节点
let currentLevel = [];
let len = queue.length;
for(let i=0; i<len; i++){
let currentNode = queue.shift();
currentLevel.push(currentNode.val);
if(currentNode.left) queue.push(currentNode.left);
if(currentNode.right) queue.push(currentNode.right);
}
result.push(currentLevel);
}
//倒序输出
for(let i=result.length - 1; i>=0; i--){
let str = "";
for(let j=result[i].length - 1; j>=0; j--){
str = str + result[i][j] + " ";
}
console.log(str.trim());
}
}
//实现一个find函数:给定一个root节点,和一个目标值,返回其路径,若无此节点则返回[];
/*
如:find(root, 1) => [1]
find(root, 7) => [1, 3, 7]
find(root, 9) => []
*/
function find(root, target){
if(root == null) return [];
return helper(root, target, []);
function helper(node,target,path){
path = Array.from(path);
//如果当前节点是结果
if(node.val == target){
return [...path,node.val];
}
if(node.left) {
let res = helper(node.left, target, [...path,node.val]);
if(res.length != 0){
return res;
}
}
if(node.right) {
let res = helper(node.right, target, [...path,node.val]);
if(res.length != 0){
return res;
}
}
return [];
}
}