一、判断为数字且部位空
//判断为数字
export function isNumber(obj) {
return typeof obj === 'number' && !isNaN(obj)
}
//判断是否为数字且不为空
export function isRealNum(val){
if(val === "" || val ==null){
return false;
}
if(!isNaN(val)){
return true;
}else{
return false;
}
}
二、二叉树的深度遍历、广度遍历
const tree = {
value: 1,
left: {
value: 2,
left: {
value: 4,
left: {
value: 8
},
right: {
value: 9
}
},
right: {
value: 5
}
},
right: {
value: 3,
left: {
value: 6
},
right: {
value: 7
}
}
}
//深度优先:
function dfsRecursion(tree) {
let res = [];
function dfs(node){
if(node) {
res.push(node.value);
dfs(node.left);
dfs(node.right);
}
}
dfs(tree);
return res;
}
dfsRecursion(tree)
//结果 [1, 2, 4, 8, 9, 5, 3, 6, 7]
//广度优先:
function bfs(tree) {
let res = [];
let arr = [];
arr.push(tree);
while(arr.length) {
const node = arr.shift();
res.push(node.value);
if(node.left) {
arr.push(node.left);
}
if(node.right) {
arr.push(node.right);
}
}
return res;
}
bfs(tree)
//结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]