力扣199,二叉树的右视图
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct TreeNode* quene[101];
int* rightSideView(struct TreeNode* root, int* returnSize) {
*returnSize = 0;
if (root == NULL) {
return NULL;
}
int front = 0, rear = 0;
int* p = (int*)malloc(sizeof(int) * 101);
quene[rear++] = root;
while (front != rear) {
int count = rear - front; //count代表这层的节点数。
while (count != 0) //表明在同一层,这里就是记录找到最右侧的,即该层中最后一个。
{
if (count == 1) //表明这是这层的最后一个
{
p[(*returnSize)++] = quene[front]->val;
}
if (quene[front]->left != NULL)
{
quene[rear] = quene[front]->left;
rear++;
}
if (quene[front]->right)
{
quene[rear] = quene[front]->right;
rear++;
}
front++; //出队
count--;
}
}
return p;
}
力扣124,二叉树的最大路径
int max ;
int getMax(struct TreeNode*root)
{
if (root == NULL)
{
return 0;
}
int left_num = fmax(0, getMax(root->left));
int right_num = fmax(0, getMax(root->right));
max = fmax(max, left_num + right_num + root->val);
return root->val + fmax(left_num , right_num);
}
int maxPathSum(struct TreeNode* root) {
max = INT_MIN;
getMax(root);
return max;
}