File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,33 @@ const pathSum = function (nums) {
3535
3636// another
3737
38+ /**
39+ * @param {number[] } nums
40+ * @return {number }
41+ */
42+ const pathSum = function ( nums ) {
43+ if ( nums == null || nums . length === 0 ) return 0
44+ const tree = new Map ( )
45+ for ( let num of nums ) {
46+ tree . set ( ( num / 10 ) >> 0 , num % 10 )
47+ }
48+ return traverse ( ( nums [ 0 ] / 10 ) >> 0 , 0 )
49+
50+ function traverse ( node , prev ) {
51+ if ( ! tree . has ( node ) ) return 0
52+ const level = ( node / 10 ) >> 0
53+ const pos = node % 10
54+ const val = tree . get ( node )
55+ const left = ( level + 1 ) * 10 + pos * 2 - 1
56+ const right = ( level + 1 ) * 10 + pos * 2
57+ const cur = prev + val
58+ if ( ! tree . has ( left ) && ! tree . has ( right ) ) return cur
59+ return traverse ( left , cur ) + traverse ( right , cur )
60+ }
61+ }
62+
63+ // another
64+
3865/**
3966 * @param {number[] } nums
4067 * @return {number }
You can’t perform that action at this time.
0 commit comments