diff --git a/README.md b/README.md index c7eb371..5ebd503 100644 --- a/README.md +++ b/README.md @@ -776,6 +776,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 0 | 100.00 | 0230 |[Kth Smallest Element in a BST](src/main/ts/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 #### Day 10 Graph/BFS/DFS @@ -945,6 +946,8 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. |-|-|-|-|-|- | 0094 |[Binary Tree Inorder Traversal](src/main/ts/g0001_0100/s0094_binary_tree_inorder_traversal/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 | 0102 |[Binary Tree Level Order Traversal](src/main/ts/g0101_0200/s0102_binary_tree_level_order_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00 +| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 +| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 0 | 100.00 | 0543 |[Diameter of Binary Tree](src/main/ts/g0501_0600/s0543_diameter_of_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 1 | 87.16 | 0226 |[Invert Binary Tree](src/main/ts/g0201_0300/s0226_invert_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 | 0104 |[Maximum Depth of Binary Tree](src/main/ts/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00 @@ -1096,7 +1099,9 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 0226 |[Invert Binary Tree](src/main/ts/g0201_0300/s0226_invert_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 | 0101 |[Symmetric Tree](src/main/ts/g0101_0200/s0101_symmetric_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00 | 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/ts/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Big_O_Time_O(N)_Space_O(N) | 2 | 93.38 +| 0106 |[Construct Binary Tree from Inorder and Postorder Traversal](src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution.ts)| Medium | Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 1 | 98.78 | 0114 |[Flatten Binary Tree to Linked List](src/main/ts/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00 +| 0112 |[Path Sum](src/main/ts/g0101_0200/s0112_path_sum/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 | 0124 |[Binary Tree Maximum Path Sum](src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 2 | 71.11 | 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/ts/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 61 | 75.97 @@ -1105,6 +1110,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- | 0102 |[Binary Tree Level Order Traversal](src/main/ts/g0101_0200/s0102_binary_tree_level_order_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00 +| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 #### Top Interview 150 Binary Search Tree @@ -1147,6 +1153,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 0 | 100.00 | 0148 |[Sort List](src/main/ts/g0101_0200/s0148_sort_list/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(log(N))_Space_O(log(N)) | 36 | 44.94 | 0023 |[Merge k Sorted Lists](src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Heap_Priority_Queue, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(k\*n\*log(k))_Space_O(log(k)) | 4 | 97.65 @@ -1207,6 +1214,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 0064 |[Minimum Path Sum](src/main/ts/g0001_0100/s0064_minimum_path_sum/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 4 | 70.73 | 0063 |[Unique Paths II](src/main/ts/g0001_0100/s0063_unique_paths_ii/solution.ts)| Medium | Array, Dynamic_Programming, Matrix | 0 | 100.00 | 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 8 | 99.14 +| 0097 |[Interleaving String](src/main/ts/g0001_0100/s0097_interleaving_string/solution.ts)| Medium | String, Dynamic_Programming | 43 | 97.65 | 0072 |[Edit Distance](src/main/ts/g0001_0100/s0072_edit_distance/solution.ts)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 6 | 93.83 | 0221 |[Maximal Square](src/main/ts/g0201_0300/s0221_maximal_square/solution.ts)| Medium | Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 18 | 59.02 @@ -1286,6 +1294,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- | 0226 |[Invert Binary Tree](src/main/ts/g0201_0300/s0226_invert_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 +| 0112 |[Path Sum](src/main/ts/g0101_0200/s0112_path_sum/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 #### Day 13 Tree @@ -1393,7 +1402,9 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 0 | 100.00 | 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/ts/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Big_O_Time_O(N)_Space_O(N) | 2 | 93.38 +| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 #### Day 16 Tree @@ -1697,11 +1708,16 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 0124 |[Binary Tree Maximum Path Sum](src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(N) | 2 | 71.11 | 0121 |[Best Time to Buy and Sell Stock](src/main/ts/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(N)_Space_O(1) | 1 | 96.44 | 0114 |[Flatten Binary Tree to Linked List](src/main/ts/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00 +| 0112 |[Path Sum](src/main/ts/g0101_0200/s0112_path_sum/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Top_Interview_150_Binary_Tree_General | 0 | 100.00 +| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Level_2_Day_9_Binary_Search_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Divide_and_Conquer | 0 | 100.00 +| 0106 |[Construct Binary Tree from Inorder and Postorder Traversal](src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution.ts)| Medium | Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Top_Interview_150_Binary_Tree_General | 1 | 98.78 | 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/ts/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(N) | 2 | 93.38 | 0104 |[Maximum Depth of Binary Tree](src/main/ts/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00 +| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_15_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_BFS | 0 | 100.00 | 0102 |[Binary Tree Level Order Traversal](src/main/ts/g0101_0200/s0102_binary_tree_level_order_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_BFS, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00 | 0101 |[Symmetric Tree](src/main/ts/g0101_0200/s0101_symmetric_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00 | 0098 |[Validate Binary Search Tree](src/main/ts/g0001_0100/s0098_validate_binary_search_tree/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00 +| 0097 |[Interleaving String](src/main/ts/g0001_0100/s0097_interleaving_string/solution.ts)| Medium | String, Dynamic_Programming, Top_Interview_150_Multidimensional_DP | 43 | 97.65 | 0096 |[Unique Binary Search Trees](src/main/ts/g0001_0100/s0096_unique_binary_search_trees/solution.ts)| Medium | Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00 | 0094 |[Binary Tree Inorder Traversal](src/main/ts/g0001_0100/s0094_binary_tree_inorder_traversal/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 | 0092 |[Reverse Linked List II](src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts)| Medium | Linked_List, Top_Interview_150_Linked_List | 0 | 100.00 diff --git a/src/main/ts/g0001_0100/s0097_interleaving_string/readme.md b/src/main/ts/g0001_0100/s0097_interleaving_string/readme.md new file mode 100644 index 0000000..3ad6417 --- /dev/null +++ b/src/main/ts/g0001_0100/s0097_interleaving_string/readme.md @@ -0,0 +1,42 @@ +97\. Interleaving String + +Medium + +Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`. + +An **interleaving** of two strings `s` and `t` is a configuration where they are divided into **non-empty** substrings such that: + +* s = s1 + s2 + ... + sn +* t = t1 + t2 + ... + tm +* `|n - m| <= 1` +* The **interleaving** is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ... + +**Note:** `a + b` is the concatenation of strings `a` and `b`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg) + +**Input:** s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" + +**Output:** true + +**Example 2:** + +**Input:** s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" + +**Output:** false + +**Example 3:** + +**Input:** s1 = "", s2 = "", s3 = "" + +**Output:** true + +**Constraints:** + +* `0 <= s1.length, s2.length <= 100` +* `0 <= s3.length <= 200` +* `s1`, `s2`, and `s3` consist of lowercase English letters. + +**Follow up:** Could you solve it using only `O(s2.length)` additional memory space? diff --git a/src/main/ts/g0001_0100/s0097_interleaving_string/solution.ts b/src/main/ts/g0001_0100/s0097_interleaving_string/solution.ts new file mode 100644 index 0000000..4823a72 --- /dev/null +++ b/src/main/ts/g0001_0100/s0097_interleaving_string/solution.ts @@ -0,0 +1,38 @@ +// #Medium #String #Dynamic_Programming #Top_Interview_150_Multidimensional_DP +// #2025_04_05_Time_43_ms_(97.65%)_Space_58.17_MB_(61.77%) + +function isInterleave(s1: string, s2: string, s3: string): boolean { + if (s3.length !== (s1.length + s2.length)) { + return false + } + const cache: boolean[][] = Array.from({ length: s1.length + 1 }, () => Array(s2.length + 1).fill(null)) + return isInterleaveHelper(s1, s2, s3, 0, 0, 0, cache) +} + +function isInterleaveHelper( + s1: string, + s2: string, + s3: string, + i1: number, + i2: number, + i3: number, + cache: boolean[][] +): boolean { + if (cache[i1][i2] !== null) { + return cache[i1][i2] + } + if (i1 === s1.length && i2 === s2.length && i3 === s3.length) { + return true + } + let result = false + if (i1 < s1.length && s1.charAt(i1) === s3.charAt(i3)) { + result = isInterleaveHelper(s1, s2, s3, i1 + 1, i2, i3 + 1, cache) + } + if (i2 < s2.length && s2.charAt(i2) === s3.charAt(i3)) { + result = result || isInterleaveHelper(s1, s2, s3, i1, i2 + 1, i3 + 1, cache) + } + cache[i1][i2] = result + return result +} + +export { isInterleave } diff --git a/src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/readme.md b/src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/readme.md new file mode 100644 index 0000000..d403d28 --- /dev/null +++ b/src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/readme.md @@ -0,0 +1,30 @@ +103\. Binary Tree Zigzag Level Order Traversal + +Medium + +Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between). + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg) + +**Input:** root = [3,9,20,null,null,15,7] + +**Output:** [[3],[20,9],[15,7]] + +**Example 2:** + +**Input:** root = [1] + +**Output:** [[1]] + +**Example 3:** + +**Input:** root = [] + +**Output:** [] + +**Constraints:** + +* The number of nodes in the tree is in the range `[0, 2000]`. +* `-100 <= Node.val <= 100` diff --git a/src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts b/src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts new file mode 100644 index 0000000..5f57a79 --- /dev/null +++ b/src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts @@ -0,0 +1,54 @@ +// #Medium #Top_Interview_Questions #Breadth_First_Search #Tree #Binary_Tree +// #Data_Structure_II_Day_15_Tree #Udemy_Tree_Stack_Queue #Top_Interview_150_Binary_Tree_BFS +// #2025_04_05_Time_0_ms_(100.00%)_Space_58.00_MB_(52.61%) + +import { TreeNode } from '../../com_github_leetcode/treenode' + +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ +function zigzagLevelOrder(root: TreeNode | null): number[][] { + const result: number[][] = [] + if (root === null) { + return result + } + const q: (TreeNode | null)[] = [root, null] + let zig = true + let level: number[] = [] + while (q.length > 0) { + const node = q.shift() + if (node === null) { + result.push(level) + zig = !zig + level = [] + if (q.length > 0) { + q.push(null) + } + } else { + if (zig) { + level.push(node.val) + } else { + level.unshift(node.val) + } + if (node.left !== null) { + q.push(node.left) + } + if (node.right !== null) { + q.push(node.right) + } + } + } + return result +} + +export { zigzagLevelOrder } diff --git a/src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/readme.md b/src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/readme.md new file mode 100644 index 0000000..65ba6b3 --- /dev/null +++ b/src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/readme.md @@ -0,0 +1,29 @@ +106\. Construct Binary Tree from Inorder and Postorder Traversal + +Medium + +Given two integer arrays `inorder` and `postorder` where `inorder` is the inorder traversal of a binary tree and `postorder` is the postorder traversal of the same tree, construct and return _the binary tree_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg) + +**Input:** inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] + +**Output:** [3,9,20,null,null,15,7] + +**Example 2:** + +**Input:** inorder = [-1], postorder = [-1] + +**Output:** [-1] + +**Constraints:** + +* `1 <= inorder.length <= 3000` +* `postorder.length == inorder.length` +* `-3000 <= inorder[i], postorder[i] <= 3000` +* `inorder` and `postorder` consist of **unique** values. +* Each value of `postorder` also appears in `inorder`. +* `inorder` is **guaranteed** to be the inorder traversal of the tree. +* `postorder` is **guaranteed** to be the postorder traversal of the tree. diff --git a/src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution.ts b/src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution.ts new file mode 100644 index 0000000..5b4a677 --- /dev/null +++ b/src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution.ts @@ -0,0 +1,36 @@ +// #Medium #Array #Hash_Table #Tree #Binary_Tree #Divide_and_Conquer +// #Top_Interview_150_Binary_Tree_General #2025_04_05_Time_1_ms_(98.78%)_Space_59.57_MB_(88.16%) + +import { TreeNode } from '../../com_github_leetcode/treenode' + +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ +function buildTree(inorder: number[], postorder: number[]): TreeNode | null { + const inIndex: number[] = [inorder.length - 1] + const postIndex: number[] = [postorder.length - 1] + return helper(inorder, postorder, inIndex, postIndex, Number.MAX_VALUE) +} + +function helper(inorder: number[], postorder: number[], inIndex: number[], postIndex: number[], target: number): TreeNode | null { + if (inIndex[0] < 0 || inorder[inIndex[0]] === target) { + return null + } + const root = new TreeNode(postorder[postIndex[0]--]) + root.right = helper(inorder, postorder, inIndex, postIndex, root.val) + inIndex[0]-- + root.left = helper(inorder, postorder, inIndex, postIndex, target) + return root +} + +export { buildTree } diff --git a/src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/readme.md b/src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/readme.md new file mode 100644 index 0000000..09b5c6e --- /dev/null +++ b/src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/readme.md @@ -0,0 +1,33 @@ +108\. Convert Sorted Array to Binary Search Tree + +Easy + +Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a **height-balanced** binary search tree_. + +A **height-balanced** binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg) + +**Input:** nums = [-10,-3,0,5,9] + +**Output:** [0,-3,9,-10,null,5] + +**Explanation:** [0,-10,5,null,-3,null,9] is also accepted: ![](https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg) + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/02/18/btree.jpg) + +**Input:** nums = [1,3] + +**Output:** [3,1] + +**Explanation:** [1,3] and [3,1] are both a height-balanced BSTs. + +**Constraints:** + +* 1 <= nums.length <= 104 +* -104 <= nums[i] <= 104 +* `nums` is sorted in a **strictly increasing** order. diff --git a/src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts b/src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts new file mode 100644 index 0000000..05dcdea --- /dev/null +++ b/src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts @@ -0,0 +1,35 @@ +// #Easy #Top_Interview_Questions #Array #Tree #Binary_Tree #Binary_Search_Tree #Divide_and_Conquer +// #Data_Structure_II_Day_15_Tree #Level_2_Day_9_Binary_Search_Tree #Udemy_Tree_Stack_Queue +// #Top_Interview_150_Divide_and_Conquer #2025_04_05_Time_0_ms_(100.00%)_Space_59.64_MB_(37.39%) + +import { TreeNode } from '../../com_github_leetcode/treenode' + +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ +function sortedArrayToBST(nums: number[]): TreeNode | null { + return makeTree(nums, 0, nums.length - 1) +} + +function makeTree(nums: number[], left: number, right: number): TreeNode | null { + if (left > right) { + return null + } + const mid = Math.floor((left + right) / 2) + const midNode = new TreeNode(nums[mid]) + midNode.left = makeTree(nums, left, mid - 1) + midNode.right = makeTree(nums, mid + 1, right) + return midNode +} + +export { sortedArrayToBST } diff --git a/src/main/ts/g0101_0200/s0112_path_sum/readme.md b/src/main/ts/g0101_0200/s0112_path_sum/readme.md new file mode 100644 index 0000000..132f5d6 --- /dev/null +++ b/src/main/ts/g0101_0200/s0112_path_sum/readme.md @@ -0,0 +1,36 @@ +112\. Path Sum + +Easy + +Given the `root` of a binary tree and an integer `targetSum`, return `true` if the tree has a **root-to-leaf** path such that adding up all the values along the path equals `targetSum`. + +A **leaf** is a node with no children. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg) + +**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 + +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg) + +**Input:** root = [1,2,3], targetSum = 5 + +**Output:** false + +**Example 3:** + +**Input:** root = [1,2], targetSum = 0 + +**Output:** false + +**Constraints:** + +* The number of nodes in the tree is in the range `[0, 5000]`. +* `-1000 <= Node.val <= 1000` +* `-1000 <= targetSum <= 1000` + \ No newline at end of file diff --git a/src/main/ts/g0101_0200/s0112_path_sum/solution.ts b/src/main/ts/g0101_0200/s0112_path_sum/solution.ts new file mode 100644 index 0000000..9a9f790 --- /dev/null +++ b/src/main/ts/g0101_0200/s0112_path_sum/solution.ts @@ -0,0 +1,29 @@ +// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Data_Structure_I_Day_12_Tree +// #Top_Interview_150_Binary_Tree_General #2025_04_05_Time_0_ms_(100.00%)_Space_59.81_MB_(41.32%) + +import { TreeNode } from '../../com_github_leetcode/treenode' + +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ +function hasPathSum(root: TreeNode | null, targetSum: number): boolean { + if (root === null) { + return false + } + if (targetSum === root.val && root.left === null && root.right === null) { + return true + } + return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val) +} + +export { hasPathSum } diff --git a/src/test/ts/g0001_0100/s0097_interleaving_string/solution.test.ts b/src/test/ts/g0001_0100/s0097_interleaving_string/solution.test.ts new file mode 100644 index 0000000..8c6335f --- /dev/null +++ b/src/test/ts/g0001_0100/s0097_interleaving_string/solution.test.ts @@ -0,0 +1,11 @@ +// tslint:disable:no-magic-numbers +import { isInterleave } from 'src/main/ts/g0001_0100/s0097_interleaving_string/solution' +import { expect, test } from 'vitest' + +test('isInterleave', () => { + expect(isInterleave('aabcc', 'dbbca', 'aadbbcbcac')).toEqual(true) +}) + +test('isInterleave2', () => { + expect(isInterleave('aabcc', 'dbbca', 'aadbbbaccc')).toEqual(false) +}) diff --git a/src/test/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.test.ts b/src/test/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.test.ts new file mode 100644 index 0000000..e20585b --- /dev/null +++ b/src/test/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.test.ts @@ -0,0 +1,16 @@ +// tslint:disable:no-magic-numbers +import { zigzagLevelOrder } from 'src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution' +import { expect, test } from 'vitest' +import { createTreeNode } from 'src/main/ts/com_github_leetcode/treenode' + +test('zigzagLevelOrder', () => { + expect(zigzagLevelOrder(createTreeNode([3, 9, 20, null, null, 15, 7]))).toEqual([[3],[20, 9],[15, 7]]) +}) + +test('zigzagLevelOrder2', () => { + expect(zigzagLevelOrder(createTreeNode([1]))).toEqual([[1]]) +}) + +test('zigzagLevelOrder3', () => { + expect(zigzagLevelOrder(createTreeNode([]))).toEqual([]) +}) diff --git a/src/test/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution.test.ts b/src/test/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution.test.ts new file mode 100644 index 0000000..8168042 --- /dev/null +++ b/src/test/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution.test.ts @@ -0,0 +1,12 @@ +// tslint:disable:no-magic-numbers +import { buildTree } from 'src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution' +import { expect, test } from 'vitest' +import { createTreeNode } from 'src/main/ts/com_github_leetcode/treenode' + +test('buildTree', () => { + expect(buildTree([9, 3, 15, 20, 7], [9, 15, 7, 20, 3])).toEqual(createTreeNode([3, 9, 20, null, null, 15, 7])) +}) + +test('buildTree', () => { + expect(buildTree([-1], [-1])).toEqual(createTreeNode([-1])) +}) diff --git a/src/test/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.test.ts b/src/test/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.test.ts new file mode 100644 index 0000000..9ca1b18 --- /dev/null +++ b/src/test/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.test.ts @@ -0,0 +1,12 @@ +// tslint:disable:no-magic-numbers +import { sortedArrayToBST } from 'src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution' +import { expect, test } from 'vitest' +import { createTreeNode } from 'src/main/ts/com_github_leetcode/treenode' + +test('sortedArrayToBST', () => { + expect(sortedArrayToBST([-10, -3, 0, 5, 9])).toEqual(createTreeNode([0, -10, 5, null, -3, null, 9])) +}) + +test('sortedArrayToBST2', () => { + expect(sortedArrayToBST([1, 3])).toEqual(createTreeNode([1, null, 3])) +}) diff --git a/src/test/ts/g0101_0200/s0112_path_sum/solution.test.ts b/src/test/ts/g0101_0200/s0112_path_sum/solution.test.ts new file mode 100644 index 0000000..9f3a658 --- /dev/null +++ b/src/test/ts/g0101_0200/s0112_path_sum/solution.test.ts @@ -0,0 +1,16 @@ +// tslint:disable:no-magic-numbers +import { hasPathSum } from 'src/main/ts/g0101_0200/s0112_path_sum/solution' +import { expect, test } from 'vitest' +import { createTreeNode } from 'src/main/ts/com_github_leetcode/treenode' + +test('hasPathSum', () => { + expect(hasPathSum(createTreeNode([5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1]), 22)).toEqual(true) +}) + +test('hasPathSum2', () => { + expect(hasPathSum(createTreeNode([1, 2, 3]), 5)).toEqual(false) +}) + +test('hasPathSum3', () => { + expect(hasPathSum(createTreeNode([]), 0)).toEqual(false) +})