字節跳動前端面試題兩道


1,多維數組扁平化。原數組[[0],[2,3,4],1,[1,[2,3]]],輸出[0,2,3,4,1,1,2,3]

{    //判斷當前數組是否有子數組
        function hasChildArray(arr) {
          return arr.some(element => {
            if (Array.isArray(element)) {
              has = true;
              return true;
            }
          });
        }
        let sourceArr = [[0], [2, 3, 4], 1, [1, [2, 3]]];
        let result = [];
     //遞歸 (
function doFunc(arr) { if (hasChildArray(arr)) { for (let i = 0, l = arr.length; i < l; i++) { if (typeof arr[i] == "number") { result.push(arr[i]); } else if (Array.isArray(arr[i])) { doFunc(arr[i]); } } } else { result=result.concat(arr); } })(sourceArr); console.log(result); }

 

2,二叉樹tree ,根節點是root,判斷是否存在一條完整路徑,其路徑上節點的值之和為target,輸出布爾值。

舉例:下面的樹,是否存在target=7的一條完整路徑(從根節點到葉節點),該路徑上各個節點之和=target?

      6
    /   \
   2     3
  /\    / 
-1  3  0

這版本1:該版本創建二叉樹的方法比較“笨”

      {
        //定義節點的數據結構
        class Node {
          constructor(value, left, right) {
            this.value = value;
            this.left = left;
            this.right = right;
          }
        }
        //定義二叉樹
        class BTree {
          constructor() {
            this.list = [];
          }
          addRoot(node) {
            if (node != null) {
              this.list.push(node);
            }
          }
          addLeft(pNode, node) {
            this.list.push(node);
            pNode.left = node;
          }
          addRight(pNode, node) {
            this.list.push(node);
            pNode.right = node;
          }
          //根據數組中的索引返回node
          getNode(index) {
            return this.list[index];
          }
          getNodeList() {
            return this.list;
          }
        }

        const lable = "MK";

        //創建示例中的二叉樹
        let bTree = new BTree();
        //第一層 根節點
        bTree.addRoot(new Node(6, null, null));
        //第二層
        bTree.addLeft(bTree.getNode(0), new Node(2, null, null));
        bTree.addRight(bTree.getNode(0), new Node(3, null, null));
        //第三層
        bTree.addLeft(bTree.getNode(1), new Node(-1, null, null));
        bTree.addRight(bTree.getNode(1), new Node(3, null, null));
        bTree.addLeft(bTree.getNode(2), new Node(0, null, null));

        function hasPathSum(node, target) {
          console.log(node.value);
          //根節點
          if (!node.left && !node.right) {
            return node.value == target;
          }
          //左右子節點
          return (
            (node.left && hasPathSum(node.left, target - node.value)) ||
            (node.right && hasPathSum(node.right, target - node.value))
          );
        }

        console.time(lable);
        console.log(hasPathSum(bTree.getNode(0), 11));
        console.timeEnd(lable);
      }

版本2:精簡版

      {
        //定義二叉樹
        class BTree {
          constructor(middle, left, right) {
            if (middle!=undefined) {
              this.value=middle;
              if (left!=undefined) this.left = left;
              if (right!=undefined) this.right = right;
            }
          }
        }

        /**
         * 創建一個樹
         * arr:一個代表二叉樹的多維數組
         */

        function makeBTree(arr) {
          if (arr) {
            if (arr.length == 1) return new BTree(arr[0], null, null);
            return new BTree(arr[0], makeBTree(arr[1]), makeBTree(arr[2]));
          }
        }

        const lable = "MK";

        //創建示例中的二叉樹
        let bTree = makeBTree([6, [2, [-1], [3]], [3, [0]]]);
        // let bTree = makeBTree([6, [2],[-1]]);

        function hasPathSum(node, target) {
          console.log(node);
          //根節點
          if (node.left==undefined && node.right==undefined) {
            return node.value == target;
          }
          //左子節點
          return (
            (node.left!=undefined && hasPathSum(node.left, target - node.value)) ||
            (node.right!=undefined && hasPathSum(node.right, target - node.value))
          );
        }

        console.time(lable);
        console.log(hasPathSum(bTree, 11));
        console.timeEnd(lable);
      }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM