File tree Expand file tree Collapse file tree 3 files changed +73
-0
lines changed
find-minimum-in-rotated-sorted-array
maximum-depth-of-binary-tree Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+ var findMin = function ( nums ) {
6+ return Math . min ( ...nums )
7+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val, left, right) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.left = (left===undefined ? null : left)
6+ * this.right = (right===undefined ? null : right)
7+ * }
8+ */
9+ /**
10+ * @param {TreeNode } root
11+ * @return {number }
12+ */
13+ var maxDepth = function ( root ) {
14+ if ( ! root ) return 0
15+
16+ const dfs = ( node , level ) => {
17+ if ( ! node ) return level ;
18+
19+ let left = level ;
20+ let right = level
21+
22+ if ( node . left ) {
23+ left = dfs ( node . left , level + 1 )
24+ }
25+
26+ if ( node . right ) {
27+ right = dfs ( node . right , level + 1 )
28+ }
29+
30+ return Math . max ( left , right ) ;
31+ }
32+
33+ return dfs ( root , 1 )
34+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * function ListNode(val, next) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.next = (next===undefined ? null : next)
6+ * }
7+ */
8+ /**
9+ * @param {ListNode } list1
10+ * @param {ListNode } list2
11+ * @return {ListNode }
12+ */
13+
14+ /**
15+ * list๋ ์ค๋ฆ ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด ์๊ธฐ ๋๋ฌธ์ ๋ ๊ฐ์ ๋ฆฌ์คํธ์ head๊ฐ ๋ ์์ ๊ฐ์ด listNode์ next์ ์์๋๋ก ๋ค์ด์ค๊ฒ ํ๋ฉด ๋ฉ๋๋ค.
16+ * list ๋ ์คํ๋๊ฐ ์๋ ๊ฒฝ์ฐ์๋ ๋๋จธ์ง ๋ค๋ฅธ ํ๋์ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
17+ * ๋ ํค๋ ์ค์์ ๋ ์์ ๊ฐ์ next์ ์ฌ๊ท์ ์ผ๋ก ๋ค์ ๋งํฌ๋ ๋ฆฌ์คํธ๋ฅผ ๋๊ฒจ์ฃผ๋ฉด์ ๋น๊ตํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค
18+ *
19+ */
20+
21+ var mergeTwoLists = function ( list1 , list2 ) {
22+ if ( ! list1 || ! list2 ) return list1 || list2
23+
24+ if ( list1 . val < list2 . val ) {
25+ list1 . next = mergeTwoLists ( list1 . next , list2 )
26+ return list1
27+ }
28+
29+ list2 . next = mergeTwoLists ( list2 . next , list1 ) ;
30+
31+ return list2 ;
32+ } ;
You canโt perform that action at this time.
0 commit comments