/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ function isCompleteTree(root) { // write code here // function maxDepth(node){ // let l = 1+maxDepth(node.left) // let r = 1+maxDepth(node.right) // return Math.max(l,r) // } // let h = maxDepth(root) if (root === null) return true; let row = [root] let temp let miss = false while(row.length>0){ temp = [] for(let item of row){ if(miss&&item) return false if(!item){ miss = true continue } temp.push(item.left) temp.push(item.right) } row = temp; } return true // let arr = []; // arr.push(root); // let res = []; // while (arr.length > 0) { // let newarr = []; // let row = []; // arr.forEach((v) => { // row.push(v.val); // v.left == null ? newarr.push(null) : newarr.push(v.left); // v.right == null ? newarr.push(null) : newarr.push(v.right); // }); // arr = newarr; // res.push(row); // } // for (i = 0; i < res.length; i++) { // for (j = 0; i < res[i].length; j++) { // if (res[i][j] === null) return false; // } // } } module.exports = { isCompleteTree: isCompleteTree, };