File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 206
206
237|[ Delete Node in a Linked List] ( ./0237-delete-node-in-a-linked-list.js ) |Easy|
207
207
238|[ Product of Array Except Self] ( ./0238-product-of-array-except-self.js ) |Medium|
208
208
242|[ Valid Anagram] ( ./0242-valid-anagram.js ) |Easy|
209
+ 257|[ Binary Tree Paths] ( ./0257-binary-tree-paths.js ) |Easy|
209
210
258|[ Add Digits] ( ./0258-add-digits.js ) |Easy|
210
211
260|[ Single Number III] ( ./0260-single-number-iii.js ) |Medium|
211
212
263|[ Ugly Number] ( ./0263-ugly-number.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 257. Binary Tree Paths
3
+ * https://leetcode.com/problems/binary-tree-paths/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given the root of a binary tree, return all root-to-leaf paths in any order.
7
+ *
8
+ * A leaf is a node with no children.
9
+ */
10
+
11
+ /**
12
+ * Definition for a binary tree node.
13
+ * function TreeNode(val, left, right) {
14
+ * this.val = (val===undefined ? 0 : val)
15
+ * this.left = (left===undefined ? null : left)
16
+ * this.right = (right===undefined ? null : right)
17
+ * }
18
+ */
19
+ /**
20
+ * @param {TreeNode } root
21
+ * @return {string[] }
22
+ */
23
+ var binaryTreePaths = function ( root ) {
24
+ return tranverse ( root ) ;
25
+ } ;
26
+
27
+ function tranverse ( node , path = [ ] , result = [ ] ) {
28
+ if ( ! node ) return ;
29
+ path . push ( node . val . toString ( ) ) ;
30
+ if ( ! node . left && ! node . right ) {
31
+ result . push ( path . join ( '->' ) ) ;
32
+ } else {
33
+ tranverse ( node . left , path . slice ( ) , result ) ;
34
+ tranverse ( node . right , path . slice ( ) , result ) ;
35
+ }
36
+ return result ;
37
+ }
You can’t perform that action at this time.
0 commit comments