GithubHelp home page GithubHelp logo

路径总和 II-113 about leetcode-javascript HOT 2 OPEN

sl1673495 avatar sl1673495 commented on August 18, 2024
路径总和 II-113

from leetcode-javascript.

Comments (2)

chengpeixin avatar chengpeixin commented on August 18, 2024

data.json

{
    "code": 1,
    "children": [
        {
            "code": 2,
            "children": [
                {
                    "code": 5,
                    "children": [
                        {
                            "code": 7
                        },
                        {
                            "code": 8
                        }
                    ]
                }
            ]
        },
        {
            "code": 3,
            "children": [
                {
                    "code": 12,
                    "children": [
                        {
                            "code": 13
                        },
                        {
                            "code": 15
                        }
                    ]
                },
                {
                    "code": 90,
                    "children": [
                        {
                            "code": 99
                        },
                        {
                            "code": 91
                        },
                        {
                            "code": 78
                        },
                        {
                            "code": 66
                        },
                        {
                            "code": 16,
                            "children": [
                                {
                                    "code": 74,
                                    "children": [
                                        {
                                            "code": 73,
                                            "children": [
                                                {
                                                    "code": 70,
                                                    "children": [
                                                        {
                                                            "code": 61
                                                        },
                                                        {
                                                            "code": 62
                                                        },
                                                        {
                                                            "code": 63,
                                                            "children": [
                                                                {
                                                                    "code": 888
                                                                },
                                                                {
                                                                    "code": 999,
                                                                    "children": [
                                                                        {
                                                                            "code": 777
                                                                        },
                                                                        {
                                                                            "code": 222
                                                                        },
                                                                        {
                                                                            "code": 111
                                                                        }
                                                                    ]
                                                                },
                                                                {
                                                                    "code": 555
                                                                }
                                                            ]
                                                        }
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "code": 4,
            "children": [
                {
                    "code": 21,
                    "children": [
                        {
                            "code": 26
                        },
                        {
                            "code": 95
                        },
                        {
                            "code": 75
                        }
                    ]
                },
                {
                    "code": 36,
                    "children": [
                        {
                            "code": 38,
                            "children": [
                                {
                                    "code": 39
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

index.js

const data = require('./data.json')


function isChildren(node){
	return node.children?true:false
}
var pathCode = function(node, code,key) {
    const pathes = [];
    _pathCode(node, code, pathes, [],key);
    return pathes;
};

function _pathCode(node, code, pathes, path,key) {
    path = [...path, node[key]];
    if (node[key]===code){
    	pathes.push(...path)
    	return 
    }
    if (isChildren(node)){
    	for (let i=0;i<node.children.length;i++){
    		_pathCode(node.children[i],code,pathes,path,key)
    	}
    }
}

const paths = pathCode(data,555,'code')

from leetcode-javascript.

vortesnail avatar vortesnail commented on August 18, 2024

大佬的思路还是很明确的,一看就懂,但是 sumVals 这个函数对于节点较多时候还是比较耗时的,可以优化一下:

var pathSum = function (root, targetSum) {
  const res = [];
  helper(root, [], 0, targetSum, res);
  return res;
};

var helper = function (node, paths, sum, targetSum, res) {
  if (!node) return;
  paths.push(node.val);
  sum += node.val;

  if (node.left) helper(node.left, paths, sum, targetSum, res);
  if (node.right) helper(node.right, paths, sum, targetSum, res);

  if (!node.left && !node.right) {
    if (sum === targetSum) {
      res.push(paths.slice());
    }
  }

  // 退出当前递归,一定要将这次递归的节点去掉,因为要回到父节点重新开启另一条路径。
  paths.pop();
  sum -= node.val;
}

from leetcode-javascript.

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.