mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
feat: add Maximum Difference Between Node and Ancestor (#1180)
* add leetcode Maximum Difference Between Node and Ancestor * chore: fix Markdown ordering Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
parent
8cbb76a02b
commit
78d083fd84
@ -104,12 +104,13 @@
|
||||
| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [C](./src/977.c) | Easy |
|
||||
| 985 | [Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries/) | [C](./src/985.c) | Medium |
|
||||
| 1009 | [Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/) | [C](./src/1009.c) | Easy |
|
||||
| 1026 | [Maximum Difference Between Node and Ancestor](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/description/) | [C](./src/1026.c) | Medium |
|
||||
| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [C](./src/1089.c) | Easy |
|
||||
| 1184 | [Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c) | Easy |
|
||||
| 1189 | [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c) | Easy |
|
||||
| 1207 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c) | Easy |
|
||||
| 1695 | [Maximum Erasure Value](https://leetcode.com/problems/maximum-erasure-value/) | [C](./src/1695.c) | Medium |
|
||||
| 1769 | [Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | [C](./src/1769.c) | Medium |
|
||||
| 1769 | [Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | [C](./src/1769.c) | Medium |
|
||||
| 1524 | [Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | [C](./src/1524.c) | Medium |
|
||||
| 1653 | [Minimum Deletions to Make String Balanced](https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/) | [C](./src/1653.c) | Medium |
|
||||
| 1704 | [Determine if String Halves Are Alike](Determine if String Halves Are Alike) | [C](./src/1704.c) | Easy |
|
||||
|
38
leetcode/src/1026.c
Normal file
38
leetcode/src/1026.c
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* struct TreeNode *left;
|
||||
* struct TreeNode *right;
|
||||
* };
|
||||
*/
|
||||
|
||||
#define max(a,b) (((a)>(b))?(a):(b))
|
||||
#define min(a,b) (((a)<(b))?(a):(b))
|
||||
|
||||
void recursiveSolve(struct TreeNode* node, int* result, int minVal, int maxVal){
|
||||
if (node == NULL){
|
||||
return;
|
||||
}
|
||||
|
||||
*result = max(*result, abs(minVal - node->val));
|
||||
*result = max(*result, abs(maxVal - node->val));
|
||||
|
||||
minVal = min(minVal, node->val);
|
||||
maxVal = max(maxVal, node->val);
|
||||
|
||||
recursiveSolve(node->left, result, minVal, maxVal);
|
||||
recursiveSolve(node->right, result, minVal, maxVal);
|
||||
}
|
||||
|
||||
// Depth First Search
|
||||
// If The maximum diff is exists it should be the difference of the MAX or MIN value in the tree path to the LEAF
|
||||
// Runtime: O(n)
|
||||
// Space: O(1)
|
||||
int maxAncestorDiff(struct TreeNode* root){
|
||||
int result = 0;
|
||||
int maxVal = root->val;
|
||||
int minVal = root->val;
|
||||
recursiveSolve(root, &result, minVal, maxVal);
|
||||
return result;
|
||||
}
|
Loading…
Reference in New Issue
Block a user