Skip to content

Commit 17d1e76

Browse files
committed
Added tasks 121-146
1 parent 5bbc5cd commit 17d1e76

File tree

11 files changed

+925
-0
lines changed

11 files changed

+925
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
2525
|-|-|-|-|-|-
26+
| 0136 |[Single Number](src/main/c/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
2627
| 0015 |[3Sum](src/main/c/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 47 | 87.75
2728

2829
#### Day 2 Array
@@ -75,6 +76,7 @@
7576
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
7677
|-|-|-|-|-|-
7778
| 0002 |[Add Two Numbers](src/main/c/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)), AI_can_be_used_to_solve_the_task | 12 | 70.50
79+
| 0142 |[Linked List Cycle II](src/main/c/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 4 | 81.55
7880

7981
#### Day 11 Linked List
8082

@@ -211,6 +213,7 @@
211213

212214
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
213215
|-|-|-|-|-|-
216+
| 0136 |[Single Number](src/main/c/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
214217

215218
### Algorithm II
216219

@@ -302,6 +305,7 @@
302305

303306
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
304307
|-|-|-|-|-|-
308+
| 0139 |[Word Break](src/main/c/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 0 | 100.00
305309

306310
#### Day 16 Dynamic Programming
307311

@@ -542,6 +546,7 @@
542546

543547
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
544548
|-|-|-|-|-|-
549+
| 0121 |[Best Time to Buy and Sell Stock](src/main/c/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
545550

546551
#### Day 8
547552

@@ -552,6 +557,7 @@
552557

553558
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
554559
|-|-|-|-|-|-
560+
| 0139 |[Word Break](src/main/c/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 0 | 100.00
555561
| 0042 |[Trapping Rain Water](src/main/c/g0001_0100/s0042_trapping_rain_water)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
556562

557563
#### Day 10
@@ -755,6 +761,7 @@
755761

756762
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
757763
|-|-|-|-|-|-
764+
| 0138 |[Copy List with Random Pointer](src/main/c/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 4 | 91.18
758765

759766
#### Day 15
760767

@@ -933,11 +940,13 @@
933940

934941
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
935942
|-|-|-|-|-|-
943+
| 0142 |[Linked List Cycle II](src/main/c/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 4 | 81.55
936944

937945
#### Day 5 Greedy
938946

939947
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
940948
|-|-|-|-|-|-
949+
| 0121 |[Best Time to Buy and Sell Stock](src/main/c/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
941950

942951
#### Day 6 Tree
943952

@@ -1111,6 +1120,7 @@
11111120

11121121
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11131122
|-|-|-|-|-|-
1123+
| 0136 |[Single Number](src/main/c/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
11141124
| 0007 |[Reverse Integer](src/main/c/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math | 0 | 100.00
11151125
| 0009 |[Palindrome Number](src/main/c/g0001_0100/s0009_palindrome_number)| Easy | Math | 0 | 100.00
11161126

@@ -1133,6 +1143,7 @@
11331143

11341144
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11351145
|-|-|-|-|-|-
1146+
| 0121 |[Best Time to Buy and Sell Stock](src/main/c/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
11361147
| 0001 |[Two Sum](src/main/c/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 2 | 99.56
11371148
| 0055 |[Jump Game](src/main/c/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
11381149
| 0075 |[Sort Colors](src/main/c/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
@@ -1171,8 +1182,12 @@
11711182
|-|-|-|-|-|-
11721183
| 0114 |[Flatten Binary Tree to Linked List](src/main/c/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| 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
11731184
| 0024 |[Swap Nodes in Pairs](src/main/c/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1185+
| 0142 |[Linked List Cycle II](src/main/c/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 4 | 81.55
1186+
| 0141 |[Linked List Cycle](src/main/c/g0101_0200/s0141_linked_list_cycle)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 6 | 92.85
11741187
| 0021 |[Merge Two Sorted Lists](src/main/c/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
1188+
| 0138 |[Copy List with Random Pointer](src/main/c/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 4 | 91.18
11751189
| 0025 |[Reverse Nodes in k-Group](src/main/c/g0001_0100/s0025_reverse_nodes_in_k_group)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
1190+
| 0146 |[LRU Cache](src/main/c/g0101_0200/s0146_lru_cache)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 37 | 91.67
11761191

11771192
#### Udemy Tree Stack Queue
11781193

@@ -1181,6 +1196,7 @@
11811196
| 0094 |[Binary Tree Inorder Traversal](src/main/c/g0001_0100/s0094_binary_tree_inorder_traversal)| 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
11821197
| 0102 |[Binary Tree Level Order Traversal](src/main/c/g0101_0200/s0102_binary_tree_level_order_traversal)| 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
11831198
| 0104 |[Maximum Depth of Binary Tree](src/main/c/g0101_0200/s0104_maximum_depth_of_binary_tree)| 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
1199+
| 0124 |[Binary Tree Maximum Path Sum](src/main/c/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
11841200
| 0098 |[Validate Binary Search Tree](src/main/c/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
11851201

11861202
#### Udemy Trie and Heap
@@ -1197,6 +1213,7 @@
11971213

11981214
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11991215
|-|-|-|-|-|-
1216+
| 0139 |[Word Break](src/main/c/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 0 | 100.00
12001217
| 0070 |[Climbing Stairs](src/main/c/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 1 | 100.00
12011218
| 0064 |[Minimum Path Sum](src/main/c/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00
12021219
| 0072 |[Edit Distance](src/main/c/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 0 | 100.00
@@ -1240,6 +1257,7 @@
12401257

12411258
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12421259
|-|-|-|-|-|-
1260+
| 0121 |[Best Time to Buy and Sell Stock](src/main/c/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
12431261

12441262
#### Day 4 Array
12451263

@@ -1261,6 +1279,7 @@
12611279

12621280
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12631281
|-|-|-|-|-|-
1282+
| 0141 |[Linked List Cycle](src/main/c/g0101_0200/s0141_linked_list_cycle)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 6 | 92.85
12641283
| 0021 |[Merge Two Sorted Lists](src/main/c/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
12651284

12661285
#### Day 8 Linked List
@@ -1308,6 +1327,16 @@
13081327

13091328
| # | Title | Difficulty | Tag | Time, ms | Time, %
13101329
|------|----------------|-------------|-------------|----------|--------
1330+
| 0146 |[LRU Cache](src/main/c/g0101_0200/s0146_lru_cache)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Udemy_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 37 | 91.67
1331+
| 0142 |[Linked List Cycle II](src/main/c/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_10_Linked_List, Level_1_Day_4_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 4 | 81.55
1332+
| 0141 |[Linked List Cycle](src/main/c/g0101_0200/s0141_linked_list_cycle)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_I_Day_7_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 6 | 92.85
1333+
| 0139 |[Word Break](src/main/c/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_9, Udemy_Dynamic_Programming, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 0 | 100.00
1334+
| 0138 |[Copy List with Random Pointer](src/main/c/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Programming_Skills_II_Day_14, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 4 | 91.18
1335+
| 0136 |[Single Number](src/main/c/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
1336+
| 0131 |[Palindrome Partitioning](src/main/c/g0101_0200/s0131_palindrome_partitioning)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 19 | 89.29
1337+
| 0128 |[Longest Consecutive Sequence](src/main/c/g0101_0200/s0128_longest_consecutive_sequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 19 | 89.29
1338+
| 0124 |[Binary Tree Maximum Path Sum](src/main/c/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
1339+
| 0121 |[Best Time to Buy and Sell Stock](src/main/c/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| 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, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
13111340
| 0114 |[Flatten Binary Tree to Linked List](src/main/c/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
13121341
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/c/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Big_O_Time_O(N)_Space_O(N) | 3 | 95.24
13131342
| 0104 |[Maximum Depth of Binary Tree](src/main/c/g0101_0200/s0104_maximum_depth_of_binary_tree)| 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, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-C/LeetCode-in-C?label=Stars&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-C/LeetCode-in-C?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C/fork)
3+
4+
## 121\. Best Time to Buy and Sell Stock
5+
6+
Easy
7+
8+
You are given an array `prices` where `prices[i]` is the price of a given stock on the <code>i<sup>th</sup></code> day.
9+
10+
You want to maximize your profit by choosing a **single day** to buy one stock and choosing a **different day in the future** to sell that stock.
11+
12+
Return _the maximum profit you can achieve from this transaction_. If you cannot achieve any profit, return `0`.
13+
14+
**Example 1:**
15+
16+
**Input:** prices = [7,1,5,3,6,4]
17+
18+
**Output:** 5
19+
20+
**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
21+
22+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
23+
24+
**Example 2:**
25+
26+
**Input:** prices = [7,6,4,3,1]
27+
28+
**Output:** 0
29+
30+
**Explanation:** In this case, no transactions are done and the max profit = 0.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
35+
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
36+
37+
## Solution
38+
39+
```c
40+
#include <stdio.h>
41+
42+
int maxProfit(int* prices, int pricesSize) {
43+
int maxProfit = 0;
44+
int min = prices[0];
45+
46+
for (int i = 1; i < pricesSize; i++) {
47+
if (prices[i] > min) {
48+
int profit = prices[i] - min;
49+
if (profit > maxProfit) {
50+
maxProfit = profit;
51+
}
52+
} else {
53+
min = prices[i];
54+
}
55+
}
56+
57+
return maxProfit;
58+
}
59+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-C/LeetCode-in-C?label=Stars&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-C/LeetCode-in-C?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C/fork)
3+
4+
## 124\. Binary Tree Maximum Path Sum
5+
6+
Hard
7+
8+
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.
9+
10+
The **path sum** of a path is the sum of the node's values in the path.
11+
12+
Given the `root` of a binary tree, return _the maximum **path sum** of any **non-empty** path_.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg)
17+
18+
**Input:** root = [1,2,3]
19+
20+
**Output:** 6
21+
22+
**Explanation:** The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg)
27+
28+
**Input:** root = [-10,9,20,null,null,15,7]
29+
30+
**Output:** 42
31+
32+
**Explanation:** The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
33+
34+
**Constraints:**
35+
36+
* The number of nodes in the tree is in the range <code>[1, 3 * 10<sup>4</sup>]</code>.
37+
* `-1000 <= Node.val <= 1000`
38+
39+
## Solution
40+
41+
```c
42+
#include <stdio.h>
43+
#include <limits.h>
44+
45+
/**
46+
* Definition for a binary tree node.
47+
* struct TreeNode {
48+
* int val;
49+
* struct TreeNode *left;
50+
* struct TreeNode *right;
51+
* };
52+
*/
53+
// Initialize max to a very low value
54+
int max = INT_MIN;
55+
56+
int helper(struct TreeNode* root) {
57+
if (root == NULL) {
58+
return 0;
59+
}
60+
61+
// Recursively get the maximum path sum for left and right subtrees
62+
int left = helper(root->left);
63+
int right = helper(root->right);
64+
65+
// Ensure negative values are replaced by 0
66+
left = (left > 0) ? left : 0;
67+
right = (right > 0) ? right : 0;
68+
69+
// Calculate the current maximum path sum through this node
70+
int current = root->val + left + right;
71+
72+
// Update global max if current is greater
73+
if (current > max) {
74+
max = current;
75+
}
76+
77+
// Return the maximum path sum including this node and one of its subtrees
78+
return root->val + ((left > right) ? left : right);
79+
}
80+
81+
int maxPathSum(struct TreeNode* root) {
82+
max = INT_MIN; // Reset max for each function call
83+
helper(root);
84+
return max;
85+
}
86+
87+
// Helper function to create a new tree node
88+
struct TreeNode* createNode(int val) {
89+
struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
90+
node->val = val;
91+
node->left = NULL;
92+
node->right = NULL;
93+
return node;
94+
}
95+
```

0 commit comments

Comments
 (0)