File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed 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 === null ) {
15+ return 0 ;
16+ } else {
17+ const leftDepth = maxDepth ( root . left ) ;
18+ const rightDepth = maxDepth ( root . right ) ;
19+
20+ return Math . max ( leftDepth , rightDepth ) + 1 ;
21+ }
22+ } ;
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+ var mergeTwoLists = function ( list1 , list2 ) {
14+ const node = new ListNode ( ) ;
15+ let tail = node ;
16+
17+ while ( list1 && list2 ) {
18+ if ( list1 . val < list2 . val ) {
19+ tail . next = list1 ;
20+ list1 = list1 . next ;
21+ } else {
22+ tail . next = list2 ;
23+ list2 = list2 . next ;
24+ }
25+ tail = tail . next ;
26+ }
27+
28+ if ( list2 ) {
29+ tail . next = list2 ;
30+ } else if ( list1 ) {
31+ tail . next = list1 ;
32+ }
33+ return node . next ;
34+ } ;
You can’t perform that action at this time.
0 commit comments