0% found this document useful (0 votes)
10 views5 pages

144 Binary Tree Preorder: Preorderhelper

Uploaded by

s0618614
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

144 Binary Tree Preorder: Preorderhelper

Uploaded by

s0618614
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

144 Binary tree preorder

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/

/**
* Helper function to perform preorder traversal and store values in an array.
*/
void preorderHelper(struct TreeNode* root, int* result, int* returnSize) {
if (root == NULL) {
return;
}

// Visit the root node


result[(*returnSize)++] = root->val;

// Traverse the left subtree


preorderHelper(root->left, result, returnSize);

// Traverse the right subtree


preorderHelper(root->right, result, returnSize);
}

/**
* Main function for preorder traversal
* LeetCode requires returning the result array and setting returnSize.
*/
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
// Allocate memory for storing traversal result
int* result = (int*)malloc(1000 * sizeof(int)); // 1000 is a large enough
size for most trees
*returnSize = 0; // Initialize the returnSize to 0

// Call the helper function to fill the result array


preorderHelper(root, result, returnSize);
return result;
}

94 Binary tree inorder

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/

/**
* Helper function to perform inorder traversal and store values in an array.
*/
void inorderHelper(struct TreeNode* root, int* result, int* returnSize) {
if (root == NULL) {
return;
}

// Traverse the left subtree


inorderHelper(root->left, result, returnSize);

// Visit the root node


result[(*returnSize)++] = root->val;

// Traverse the right subtree


inorderHelper(root->right, result, returnSize);
}

/**
* Main function for inorder traversal
* LeetCode requires returning the result array and setting returnSize.
*/
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
// Allocate memory for storing traversal result
int* result = (int*)malloc(1000 * sizeof(int)); // Allocate enough space
for the result
*returnSize = 0; // Initialize returnSize to 0
// Call the helper function to fill the result array
inorderHelper(root, result, returnSize);

return result;
}

145 Binary tree postorder

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/

/**
* Helper function to perform postorder traversal and store values in an
array.
*/
void postorderHelper(struct TreeNode* root, int* result, int* returnSize) {
if (root == NULL) {
return;
}

// Traverse the left subtree


postorderHelper(root->left, result, returnSize);

// Traverse the right subtree


postorderHelper(root->right, result, returnSize);

// Visit the root node


result[(*returnSize)++] = root->val;
}

/**
* Main function for postorder traversal
* LeetCode requires returning the result array and setting returnSize.
*/
int* postorderTraversal(struct TreeNode* root, int* returnSize) {
// Allocate memory for storing traversal result
int* result = (int*)malloc(1000 * sizeof(int)); // Allocate enough space
for the result
*returnSize = 0; // Initialize returnSize to 0

// Call the helper function to fill the result array


postorderHelper(root, result, returnSize);

return result;
}

203 Remove linked list

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/

struct ListNode* removeElements(struct ListNode* head, int val) {


// Create a dummy node that points to the head
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct
ListNode));
dummy->next = head;

struct ListNode* current = dummy; // Start from the dummy node

// Traverse the list


while (current->next != NULL) {
if (current->next->val == val) {
// Skip the node with the target value
struct ListNode* temp = current->next;
current->next = current->next->next;
free(temp); // Free memory of the removed node
} else {
current = current->next; // Move to the next node
}
}

// Update head to the new start of the list


struct ListNode* newHead = dummy->next;
free(dummy); // Free the dummy node
return newHead;
}

628 maximum product of 3 numbers

#include <stdlib.h>

int compare(const void* a, const void* b) {


return (*(int*)a - *(int*)b);
}

int maximumProduct(int* nums, int numsSize) {


// Sort the array
qsort(nums, numsSize, sizeof(int), compare);

// Calculate the product of the three largest numbers


int product1 = nums[numsSize - 1] * nums[numsSize - 2] * nums[numsSize -
3];

// Calculate the product of the two smallest numbers and the largest
number
int product2 = nums[0] * nums[1] * nums[numsSize - 1];

// Return the maximum of the two products


return product1 > product2 ? product1 : product2;
}

You might also like