亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

面試題:給你個id,去拿到name,多叉樹遍歷

jayce / 2837人閱讀

前天面試遇到一個多叉樹面試的題目,在這里分享記錄一下。

題目:一個樹形的數(shù)據(jù)(如下數(shù)據(jù)),面試官給你一個id,然后拿到對應(yīng)的name?

數(shù)據(jù)結(jié)構(gòu)大概是這個樣子

var cityData = [
      {
        id: 1,
        name: "廣東省",
        children: [
          {
            id: 11,
            name: "深圳",
            children: [
              {
                id: 111,
                name: "寶安",
                children: [
                  {
                    id: 1111,
                    name: "西鄉(xiāng)",
                    children:[
                      {
                        id: 11111,
                        name: "坪洲",
                        children:[]
                      },
                      {
                        id: 11112,
                        name: "靈芝",
                        children:[]
                      }
                    ]
                  },
                  {
                    id: 1112,
                    name: "南山",
                    children:[
                      {
                        id: 11121,
                        name: "科技園",
                        children:[]
                      }
                    ]
                  }
                ]
              },
              {
                id: 112,
                name: "福田",
                children: []
              }
            ]
          },
          {
            id: 12,
            name: "廣州",
            children: [
              {
                id: 122,
                name: "白云區(qū)",
                children: [
                  {
                    id: 1222,
                    name: "白云區(qū)",
                    children: []
                  }
                ]
              },
              {
                id: 122,
                name: "珠海區(qū)",
                children: []
              }
            ]
          }
        ]
      },
      {
        id: 2,
        name: "湖南省",
        children: []
      }
    ];

比如說 我要id11112name返回是靈芝,請問你有幾種解法??

遞歸方法

這題目讓人看到這不就是考用遞歸的方法嘛,代碼如下

let result = ""

// 遞歸實現(xiàn)
const recursion = (cityData, id) => {
  // cityData數(shù)據(jù)為空的時候直接返回
  if (!cityData || !cityData.length) return;
  // 常規(guī)循環(huán)cityData
  for (let i = 0, len = cityData.length; i < len; i++) {
    const childs = cityData[i].children;
    
    // 如果匹配到id的話,就是我們要的結(jié)果
    if (cityData[i].id === id) {
      result = cityData[i].name
    }
    // 如果還有子節(jié)點,執(zhí)行遞歸
    if(childs && childs.length > 0){
      recursion(childs, id);
    }
  }
  return result
};

const r = recursion(cityData, 11112);
console.log(r) // 靈芝

oyes~~~完成了么??面試官可能不滿意哦,下面還有幾種解法

廣度優(yōu)先實現(xiàn)
let result = ""

const range = (cityData, id) => {
  if (!cityData || !cityData.length) return;
  // 定義一個數(shù)據(jù)棧
  let stack = [];

  let item = null;

  //先將第一層節(jié)點放入棧
  for (var i = 0, len = cityData.length; i < len; i++) {
    stack.push(cityData[i]);
  }

  while (stack.length) {
    // 將數(shù)據(jù)棧的第一個取出來
    item = stack.shift();
    // 如果符合就賦值給result
    if (item.id === id) {
      result = item.name
    }
    //如果該節(jié)點有子節(jié)點,繼續(xù)添加進入棧底
    if (item.children && item.children.length) {
      stack = stack.concat(item.children);
    }
  }
  return result
};

let r1 = range(cityData, 11112);

console.log(r1) // 靈芝
深度優(yōu)先實現(xiàn)
let result = ""

const deep = (cityData, id) => {
  // 沒有數(shù)據(jù)直接返回
  if (!cityData || !cityData.length) return;
  // 先定義一個數(shù)據(jù)棧
  let stack = []
  let item = null

  //先將第一層節(jié)點放入數(shù)據(jù)棧
  for (var i = 0, len = cityData.length; i < len; i++) {
    stack.push(cityData[i])
  }
  // 循環(huán)
  while (stack.length) {
    item = stack.shift()
    if (item.id === id) {
      result = item.name
    }
    //如果該節(jié)點有子節(jié)點,繼續(xù)添加進入棧頂
    if (item.children && item.children.length) {
      // 注意這里調(diào)換了順序
      stack = item.children.concat(stack);
    }
  }
  return result
};

let r3 = deep(cityData, 11112)
console.log(r3) // 靈芝
正則方式實現(xiàn)
const regular = (cityData, id) => {
  // 沒有數(shù)據(jù)直接返回
  if (!cityData || !cityData.length) return;
  // 數(shù)據(jù)轉(zhuǎn)成字符串
  let cityStr = JSON.stringify(cityData)
  // 定義正則
  let reg = new RegExp(`"id":${id},"name":"([^x00-xff]+)",`)
  // 取到正則的子字符串并返回
  return (cityStr.match(reg))[1]
}

let r4 = regular(cityData, 11112);

console.log(r4) // 靈芝

這里列舉了4種方法,應(yīng)該還有很多種方法,大佬們有的話可以留言給我,先謝謝啦~~

安利一波博客~~~https://github.com/naihe138/naice-blog

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/94220.html

相關(guān)文章

  • 遍歷叉樹(遞歸、非遞歸廣度優(yōu)先、深度優(yōu)先)

    簡單的遍歷一個樹形結(jié)構(gòu)數(shù)據(jù)的幾種方法、非遞歸方法效率最好。 (function (window, undefined) { var treeNodes = [ { id: 1, name: 1, children: [ { i...

    wing324 評論0 收藏0
  • 字節(jié)跳動上海DATA部門后端開發(fā)秋招面試經(jīng)歷

    摘要:講了一下我在電力物聯(lián)網(wǎng)項目中通過設(shè)計的文件遠(yuǎn)程升級功能。完成聊天畢業(yè)規(guī)劃怎么樣收到面試調(diào)查問卷等待中。。。。。 7.31 投遞提前批c++客戶端崗位 8.16 被轉(zhuǎn)...

    Ocean 評論0 收藏0
  • js叉樹的分析及實現(xiàn)

    摘要:多叉樹的分析及實現(xiàn)好了,終于回到了第一篇文章提到的組織結(jié)構(gòu)的多叉樹實現(xiàn),有了前兩篇文章的基礎(chǔ),多叉樹的實現(xiàn)也就變得簡單了從后臺拿到的原始數(shù)據(jù)形式為總部工程部工程部工程部工程部測試部測試部測試部生產(chǎn)部規(guī)劃部市場部這是一個典型的多叉樹結(jié)構(gòu),總部 js多叉樹的分析及實現(xiàn) 好了,終于回到了第一篇文章提到的組織結(jié)構(gòu)的多叉樹實現(xiàn),有了前兩篇文章的基礎(chǔ),多叉樹的實現(xiàn)也就變得簡單了 從后臺拿到的原始數(shù)...

    lucas 評論0 收藏0

發(fā)表評論

0條評論

jayce

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<