144 Binary Tree Preorder: Preorderhelper
144 Binary Tree Preorder: Preorderhelper
/**
* 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;
}
/**
* 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
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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
return result;
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#include <stdlib.h>
// Calculate the product of the two smallest numbers and the largest
number
int product2 = nums[0] * nums[1] * nums[numsSize - 1];