海康威视笔试vue9.14

想不通为什么深搜有问题,求某佬指点一下...
function findNode(tree, target) {
  if (tree.name === target) {
    return tree.val;
  }

  if (tree.children) {
    for (const child of tree.children) {
      const result = findNode(child, target);
      if (result) {
        return result;
      }
    }
  }

  return null;
}

// 输入的树结构
const tree = {
  val: 'rootVal',
  name: 'rootName',
  children: [
    {
      val: 'childVal',
      name: 'childName',
      children: [
        {
          val: 'child1-1Val',
          name: 'child1-1Name'
        }
      ]
    },
    {
      val: 'child2Val',
      name: 'child2Name'
    }
  ]
};

// 输入的目标节点名称
const target = 'rootName';

const result = findNode(tree, target);
console.log(result);
全部评论
兄弟,试试光伏电池行业~
点赞
送花
回复
分享
发布于 2023-09-14 21:04 浙江

相关推荐

头像
不愿透露姓名的神秘牛友
05-08 18:16
浪潮信息 结构设计 12*15 硕士211
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务