[Leetcode -637. Level average of binary tree -671. The second smallest node in the binary tree]

Leetcode -637. Level average of binary tree

Question : Given the root node root of a non-empty binary tree, return the average value of nodes at each level in the form of an array. Answers within 10^(- 5) of the actual answer are accepted.

Example 1:
Insert image description here
Input: root = [3, 9, 20, null, null, 15, 7]
Output: [3.00000, 14.50000, 11.00000]
Explanation: The average value of layer 0 is 3, and the average value of layer 1 is 14.5 , the average value of layer 2 is 11.
Thus returning [3, 14.5, 11] .

Example 2:
Insert image description here

Input: root = [3, 9, 20, 15, 7]
Output: [3.00000, 14.50000, 11.00000]

Tip:
The number of nodes in the tree is in the range [1, 104]

  • 2^31 <= Node.val <= 2^31 - 1

Idea : create a total of three arrays, the SumVal array records the sum of nodes in each layer; the CountLevels array records the number of nodes in each layer; the ret array records the average value of each layer, that is, use the first two arrays to calculate and put them in In the ret array, return at the end;

		void dfs(struct TreeNode* root, double* SumVal, int* CountLevels, int level, int* posSumSize, int* posCountSize)
		{
		    if (root == NULL)
		        return;
		
		    // level 是记录当前节点所在的层数
		    if (level < *posSumSize)
		    {
		        SumVal[level] += (double)root->val;
		        CountLevels[level] += 1;
		    }
		
		    //每次递归到新的一层,就会将最左边的节点的值累加到下标为 posSumSize 位置的 SumVal 数组中
		    //并将 CountLevels 数组中下标为 posCountSize 的位置置1
		    else
		    {
		        SumVal[(*posSumSize)++] = (double)root->val;
		        CountLevels[(*posCountSize)++] = 1;
		    }
		    
		    //递归其左子树和右子树,层数加一
		    dfs(root->left, SumVal, CountLevels, level + 1, posSumSize, posCountSize);
		    dfs(root->right, SumVal, CountLevels, level + 1, posSumSize, posCountSize);
		
		}
		
		double* averageOfLevels(struct TreeNode* root, int* returnSize)
		{
		    //SumVal 数组存放每一层的节点和;CountLevels 数组存放每一层的节点数
		    double* SumVal = (double*)malloc(sizeof(double) * 10000);
		    int* CountLevels = (int*)malloc(sizeof(int) * 10000);
		
		    //posSumSize 和 posCountSize 分别记录两个数组的长度
		    int posSumSize = 0, posCountSize = 0;
		
		    //深度优先搜索
		    dfs(root, SumVal, CountLevels, 0, &posSumSize, &posCountSize);
		
		    //返回数组的长度
		    *returnSize = posSumSize;
		
		    //ret 数组存放每一层的平均值
		    double* ret = (double*)malloc(sizeof(double) * posSumSize);
		    for (int i = 0; i < posSumSize; i++)
		    {
		        ret[i] = SumVal[i] / CountLevels[i];
		    }
		
		    return ret;
		}

Leetcode -671.The second smallest node in the binary tree

Question : Given a non-empty special binary tree, each node is a positive number, and the number of child nodes of each node can only be 2 or 0. If a node has two child nodes, then the value of the node is equal to the smaller of the two child nodes.

More formally, root.val = min(root.left.val, root.right.val) always holds.
Given such a binary tree, you need to output the second smallest value among all nodes.
If the second smallest value does not exist, output - 1.

Example 1:
Insert image description here

Input: root = [2, 2, 5, null, null, 5, 7]
Output: 5
Explanation: The smallest value is 2 and the second smallest value is 5.

Example 2:
Insert image description here

Input: root = [2, 2, 2]
Output: - 1
Explanation: The smallest value is 2, but there is no second smallest value.

Tip:
The number of nodes in the tree is in the range [1, 25]
1 <= Node.val <= 2^31 - 1.
For each node in the tree root.val == min(root.left.val, root.right. val)

Idea : Because each node in the tree satisfies root.val == min(root.left.val, root.right.val); so the root node is the smallest node, we need to find the node larger than the root node The smallest node is the second smallest node in the tree;

		void dfs(struct TreeNode* root, int* ret, int rootval)
		{
		    if (root == NULL)
		        return;
		
		    //如果 ret 已被更改,且当前节点的值大于当前 ret 的值,就返回
		    //因为 ret 要找的是比根节点大的值中最小的值
		    if (*ret != -1 && root->val >= *ret)
		        return;
		
		    // rootval 是根节点的值,根节点就是最小的值,找出比它大的数中最小的那个即可
		    // 如果节点的值比当前 ret 大,会在上面直接返回
		    if (root->val > rootval)
		        *ret = root->val;
		
		    //递归其左子树和右子树
		    dfs(root->left, ret, rootval);
		    dfs(root->right, ret, rootval);
		}
		
		int findSecondMinimumValue(struct TreeNode* root)
		{
		    //返回值初始化为 -1
		    int ret = -1;
		    dfs(root, &ret, root->val);
		    return ret;
		}

Guess you like

Origin blog.csdn.net/YoungMLet/article/details/131407912