题目:
题解:
#define MAX_NODE_NUM 100
int* rightSideView(struct TreeNode* root, int* returnSize){
if (root == NULL) {
*returnSize = 0;
return NULL;
}
int *res = (int *)malloc(sizeof(int) * MAX_NODE_NUM);
int cnt = 0;
struct TreeNode **record = (struct TreeNode **)malloc(sizeof(struct TreeNode *) * MAX_NODE_NUM);
int head = 1;
int tail = 0;
record[0] = root;
while (head > tail) {
int turn = head - tail;
for (int i = 0; i < turn; i++) {
struct TreeNode *cur = record[tail + i];
if (cur->left != NULL) {
record[head++] = cur->left;
}
if (cur->right != NULL) {
record[head++] = cur->right;
}
if (i == turn - 1) {
res[cnt++] = cur->val;
}
}
tail += turn;
}
*returnSize = cnt;
return res;
}