Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> ans;
vector<TreeNode*> q;
vector<TreeNode*> next;
if (root == NULL) {
return ans;
}
q.push_back(root);
ans.push_back(root -> val);
long sum = 0;
for (int i = 0; i < q.size(); i++) {
TreeNode *tmp = q[i];
if (tmp -> left) {
next.push_back(tmp -> left);
sum += tmp -> left -> val;
}
if (tmp -> right) {
next.push_back(tmp -> right);
sum += tmp -> right -> val;
}
if (i == q.size() - 1) {
if (next.size() != 0) {
ans.push_back((double)sum / next.size());
}
q = next;
next.clear();
i = -1;
sum = 0;
}
}
return ans;
}
};
============== USing queue and figure out the size in the beginning =====
class Solution { public: vector<double> averageOfLevels(TreeNode* root) { vector<double> values_to_return; if(root == nullptr) return values_to_return; queue<TreeNode*> levels; levels.push(root); values_to_return.push_back(root->val); int which_level = 0; while(!levels.empty()){ which_level = levels.size(); vector<double> tmp; while(which_level>0){ TreeNode * node = levels.front(); levels.pop(); if(node->left){ levels.push(node->left); tmp.push_back(node->left->val); } if(node->right){ levels.push(node->right); tmp.push_back(node->right->val); } which_level--; } double sum = 0; if(tmp.size()>0){ for(int i =0; i<tmp.size(); ++i){ sum = tmp[i]+sum; } values_to_return.push_back(sum/tmp.size()); } } return values_to_return; } };
Example 1:
Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].
Note:
- The range of node's value is in the range of 32-bit signed integer.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> ans;
vector<TreeNode*> q;
vector<TreeNode*> next;
if (root == NULL) {
return ans;
}
q.push_back(root);
ans.push_back(root -> val);
long sum = 0;
for (int i = 0; i < q.size(); i++) {
TreeNode *tmp = q[i];
if (tmp -> left) {
next.push_back(tmp -> left);
sum += tmp -> left -> val;
}
if (tmp -> right) {
next.push_back(tmp -> right);
sum += tmp -> right -> val;
}
if (i == q.size() - 1) {
if (next.size() != 0) {
ans.push_back((double)sum / next.size());
}
q = next;
next.clear();
i = -1;
sum = 0;
}
}
return ans;
}
};
============== USing queue and figure out the size in the beginning =====
class Solution { public: vector<double> averageOfLevels(TreeNode* root) { vector<double> values_to_return; if(root == nullptr) return values_to_return; queue<TreeNode*> levels; levels.push(root); values_to_return.push_back(root->val); int which_level = 0; while(!levels.empty()){ which_level = levels.size(); vector<double> tmp; while(which_level>0){ TreeNode * node = levels.front(); levels.pop(); if(node->left){ levels.push(node->left); tmp.push_back(node->left->val); } if(node->right){ levels.push(node->right); tmp.push_back(node->right->val); } which_level--; } double sum = 0; if(tmp.size()>0){ for(int i =0; i<tmp.size(); ++i){ sum = tmp[i]+sum; } values_to_return.push_back(sum/tmp.size()); } } return values_to_return; } };
No comments:
Post a Comment