-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path199_binary_tree_right_side_view.c
53 lines (44 loc) · 1.41 KB
/
199_binary_tree_right_side_view.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <assert.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
#define SIZE 100
void rightSideViewRecursively(struct TreeNode* node, int level, int* vals, int* returnSize) {
if(node == NULL)
return;
if(*returnSize == level)
vals[(*returnSize)++] = node->val;
rightSideViewRecursively(node->right, level+1, vals, returnSize);
rightSideViewRecursively(node->left, level+1, vals, returnSize);
}
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* rightSideView(struct TreeNode* root, int* returnSize) {
int* vals = (int *)malloc(sizeof(int) * SIZE);
rightSideViewRecursively(root, 0, vals, returnSize);
return vals;
}
int main() {
struct TreeNode* root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = 1;
struct TreeNode* node1_1 = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node1_1->val = 2;
node1_1->left = NULL;
node1_1->right = NULL;
root->left = node1_1;
struct TreeNode* node1_2 = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node1_2->val = 3;
node1_2->left = NULL;
node1_2->right = NULL;
root->right = node1_2;
int returnSize = 0;
int* vals = rightSideView(root, &returnSize);
assert(vals[0] == 1);
assert(vals[1] == 3);
assert(returnSize == 2);
}