mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Added solution to leetcode problem 173 in C - found in leetcode/src/173.c
This commit is contained in:
parent
8963e54237
commit
3a21e49ecd
@ -30,6 +30,7 @@ LeetCode
|
||||
|153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [C](./src/153.c)|Medium|
|
||||
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [C](./src/160.c)|Easy|
|
||||
|169|[Majority Element](https://leetcode.com/problems/majority-element/) | [C](./src/169.c)|Easy|
|
||||
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/) | [C](./src/173.c)|Medium|
|
||||
|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [C](./src/203.c)|Easy|
|
||||
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [C](./src/206.c)|Easy|
|
||||
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C](./src/215.c)|Medium|
|
||||
|
70
leetcode/src/173.c
Normal file
70
leetcode/src/173.c
Normal file
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* struct TreeNode *left;
|
||||
* struct TreeNode *right;
|
||||
* };
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
typedef struct {
|
||||
int *values;
|
||||
int CurrentIndex;
|
||||
int NumberOfNodes;
|
||||
} BSTIterator;
|
||||
|
||||
void TraverseAndAssign(struct TreeNode *root, BSTIterator *obj) {
|
||||
if(!root)
|
||||
return;
|
||||
if(root->left)
|
||||
TraverseAndAssign(root->left, obj);
|
||||
obj->values[obj->CurrentIndex] = root->val;
|
||||
obj->CurrentIndex++;
|
||||
if(root->right)
|
||||
TraverseAndAssign(root->right, obj);
|
||||
}
|
||||
|
||||
int TotalNodes(struct TreeNode *root) {
|
||||
if(!root)
|
||||
return 0;
|
||||
int nodes_left = TotalNodes(root->left);
|
||||
int nodes_right = TotalNodes(root->right);
|
||||
return nodes_left + nodes_right + 1;
|
||||
}
|
||||
|
||||
BSTIterator* bSTIteratorCreate(struct TreeNode* root) {
|
||||
int n = TotalNodes(root);
|
||||
int size = n+1;
|
||||
printf("%d", size);
|
||||
BSTIterator *obj = (BSTIterator*)malloc(sizeof(BSTIterator));
|
||||
obj->values = (int*)calloc(size, sizeof(int));
|
||||
obj->CurrentIndex = 0;
|
||||
obj->NumberOfNodes = n;
|
||||
obj->values[size - 1] = INT_MAX;
|
||||
TraverseAndAssign(root, obj);
|
||||
obj->CurrentIndex = 0;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/** @return the next smallest number */
|
||||
int bSTIteratorNext(BSTIterator* obj) {
|
||||
int NextValue = obj->values[obj->CurrentIndex];
|
||||
obj->CurrentIndex++;
|
||||
return NextValue;
|
||||
}
|
||||
|
||||
/** @return whether we have a next smallest number */
|
||||
bool bSTIteratorHasNext(BSTIterator* obj) {
|
||||
if(!obj->NumberOfNodes) {
|
||||
return false;
|
||||
}
|
||||
printf(" Here ");
|
||||
return (obj->values[obj->CurrentIndex] == INT_MAX) ? false : true;
|
||||
}
|
||||
|
||||
void bSTIteratorFree(BSTIterator* obj) {
|
||||
free(obj->values);
|
||||
free(obj);
|
||||
}
|
Loading…
Reference in New Issue
Block a user