LEETCODE 113 (JAVASCRIPT) | PATH SUM II

  Рет қаралды 2,508

Andy Gala

Andy Gala

3 жыл бұрын

Hey everyone. Check out this in-depth solution for leetcode 113.

Пікірлер: 4
@sajalgupta7345
@sajalgupta7345 2 жыл бұрын
Hey Andy, Why did we do slate.slice() on line 23. Wouldn't slate and slate.slice() be the same thing as both will returns the array only.
@sourenasahraian2055
@sourenasahraian2055 2 жыл бұрын
They are but only at that point, Arrays are passed around by reference , so any change you do the same slate instance as it gets passed around up and down the tree(future push /pop ) are also reflected into the result . You want your hard earned path solutions to be snapshots in time and remain intact . If you don't copy the array , the same address in memory gets passed around and your data will get corrupt.
@TekkenRoom
@TekkenRoom Жыл бұрын
that's also an alternative line " paths.push([...slate])"
@user-pm2kx1vi1e
@user-pm2kx1vi1e 7 ай бұрын
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode} root * @param {number} targetSum * @return {number[][]} */ var pathSum = function(root, targetSum) { if(!root) return []; const paths=[]; const dfs = (node, sum, slate) => { if(node.left === null && node.right === null){ if(sum === node.val){ slate.push(node.val); paths.push(slate.slice()); slate.pop(); } } if(node.left){ slate.push(node.val); dfs(node.left, sum - node.val, slate); slate.pop(); } if(node.right){ slate.push(node.val); dfs(node.left, sum - node.val, slate); slate.pop(); } } dfs(root, targetSum, []); return paths; };;
LEETCODE 112 (JAVASCRIPT) | PATH SUM
9:46
Andy Gala
Рет қаралды 2,9 М.
What it feels like cleaning up after a toddler.
00:40
Daniel LaBelle
Рет қаралды 90 МЛН
Как бесплатно замутить iphone 15 pro max
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН
WORLD'S SHORTEST WOMAN
00:58
Stokes Twins
Рет қаралды 126 МЛН
LEETCODE 543 (JAVASCRIPT) | DIAMETER OF BINARY TREE
11:11
Andy Gala
Рет қаралды 3,3 М.
P vs. NP - The Biggest Unsolved Problem in Computer Science
15:33
Up and Atom
Рет қаралды 942 М.
Function Pointers in C++
12:41
The Cherno
Рет қаралды 385 М.
Why Do Bubbles Form In Glasses Of Water?
12:33
Joe Scott
Рет қаралды 80 М.
Google Coding Interview With A Facebook Software Engineer
49:59
Clément Mihailescu
Рет қаралды 929 М.
Jump Game II - Greedy - Leetcode 45 - Python
11:58
NeetCode
Рет қаралды 182 М.
Async JavaScript & Callback Functions -- Tutorial for Beginners
24:21