File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 40
40
74|[ Search a 2D Matrix] ( ./0074-search-a-2d-matrix.js ) |Medium|
41
41
83|[ Remove Duplicates from Sorted List] ( ./0083-remove-duplicates-from-sorted-list.js ) |Easy|
42
42
88|[ Merge Sorted Array] ( ./0088-merge-sorted-array.js ) |Easy|
43
+ 94|[ Binary Tree Inorder Traversal] ( ./0094-binary-tree-inorder-traversal.js ) |Easy|
43
44
118|[ Pascal's Triangle] ( ./0118-pascals-triangle.js ) |Easy|
44
45
119|[ Pascal's Triangle II] ( ./0119-pascals-triangle-ii.js ) |Easy|
45
46
121|[ Best Time to Buy and Sell Stock] ( ./0121-best-time-to-buy-and-sell-stock.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 94. Binary Tree Inorder Traversal
3
+ * https://leetcode.com/problems/binary-tree-inorder-traversal/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given the root of a binary tree, return the inorder traversal of its nodes' values.
7
+ */
8
+
9
+ /**
10
+ * Definition for a binary tree node.
11
+ * function TreeNode(val, left, right) {
12
+ * this.val = (val===undefined ? 0 : val)
13
+ * this.left = (left===undefined ? null : left)
14
+ * this.right = (right===undefined ? null : right)
15
+ * }
16
+ */
17
+ /**
18
+ * @param {TreeNode } root
19
+ * @return {number[] }
20
+ */
21
+ var inorderTraversal = function ( root ) {
22
+ const result = [ ] ;
23
+ const stack = [ ] ;
24
+
25
+ while ( root || stack . length ) {
26
+ if ( root ) {
27
+ stack . push ( root ) ;
28
+ root = root . left ;
29
+ } else {
30
+ const { val, right } = stack . pop ( ) ;
31
+ result . push ( val ) ;
32
+ root = right ;
33
+ }
34
+ }
35
+
36
+ return result ;
37
+ } ;
You can’t perform that action at this time.
0 commit comments