Giter VIP home page Giter VIP logo

Comments (5)

suukii avatar suukii commented on May 27, 2024
const findAncestors = (list, target, path = []) => {
    if (!list || list.length === 0) return [];

    for (let item of list) {
        if (item.id === target) return [...path];
        const res = findAncestors(item.children, target, [...path, item.id]);
        if (res.length > 0) return res;
    }
    return [];
};

from fe-interview.

azl397985856 avatar azl397985856 commented on May 27, 2024

思路

基本的回溯思路,只不过这题值需要求出一个满足条件的解即可,但是我还是套用了多个解的模板。

那你觉得应该如何优化呢?

代码(JS)

function main(id, nodes) {
  ans = [];
  function dfs(node, path) {
    if (node.id === id) return (ans = [...path]);
    path.push(node.id);
    for (const child of node.children || []) dfs(child, path);
    path.pop(node.id);
  }

  for (const node of nodes) {
    dfs(node, []);
    if (ans.length > 0) return ans;
  }
  return [];
}

复杂度分析

  • 时间复杂度:$O(N)$,其中 N 为节点总数。
  • 空间复杂度:$O(N)$,最坏的情况是一个类链表的结构,我们找到最后一个才找到。

更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经35K star啦。

关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。

from fe-interview.

hanxuanliang avatar hanxuanliang commented on May 27, 2024
const findAncestors = (list, target, path = []) => {
    if (!list || list.length === 0) return [];

    for (let item of list) {
        if (item.id === target) return [...path];
        const res = findAncestors(item.children, target, [...path, item.id]);
        if (res.length > 0) return res;
    }
    return [];
};

参数没懂是什么意思,说明一下?:apple:

from fe-interview.

suukii avatar suukii commented on May 27, 2024

@hanxuanliang target已知id221path 是从根节点到 target 走过的节点id

from fe-interview.

stale avatar stale commented on May 27, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from fe-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.