今日任务
二叉树的递归遍历(前中后)
二叉树的迭代遍历(前中后)
二叉树的统一迭代遍历
二叉树的层序遍历(共十道题目)
完成情况
递归已掌握,代码略
迭代前中手写一编,后和统一未学习
层序遍历题目如下
102.二叉树的层序遍历
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 vector<vector<int>> levelOrder(TreeNode* root) { 15 queue<TreeNode*>que; 16 vector<vector<int>> result; 17 if(root!=NULL) que.push(root); 18 while(!que.empty()) 19 { 20 int size=que.size(); 21 vector<int> res;//写到for循环外边 22 for(int i=0;i<size;i++) 23 { 24 25 TreeNode* cur=que.front(); 26 que.pop(); 27 res.push_back(cur->val); 28 if(cur->left) que.push(cur->left); 29 if(cur->right) que.push(cur->right); 30 31 } 32 result.push_back(res); 33 }
reverse(result.begin(),result.end());//107.层序遍历II只要加上反转 34 return result; 35 } 36 };
199.二叉树的右视图
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 vector<int> rightSideView(TreeNode* root) { 15 queue<TreeNode*> que; 16 if (root != NULL) que.push(root); 17 vector<int> result; 18 while (!que.empty()) { 19 int size = que.size(); 20 for (int i = 0; i < size; i++) { 21 TreeNode* node = que.front(); 22 que.pop(); 23 if (i == (size - 1)) result.push_back(node->val); // 将每一层的最后元素放入result数组中 24 if (node->left) que.push(node->left); 25 if (node->right) que.push(node->right); 26 } 27 } 28 return result; 29 30 } 31 };
637.二叉树的层平均值
注意精度要求
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 vector<double> averageOfLevels(TreeNode* root) { 15 queue<TreeNode*> que; 16 if (root != NULL) que.push(root); 17 vector<double> result; 18 while (!que.empty()) { 19 int size = que.size(); 20 vector<double> vec; 21 for (int i = 0; i < size; i++) { 22 TreeNode* node = que.front(); 23 que.pop(); 24 vec.push_back(node->val); 25 if (node->left) que.push(node->left); 26 if (node->right) que.push(node->right); 27 } 28 double sum=0; 29 for(int i=0;i<vec.size();i++) 30 sum+=vec[i]; 31 result.push_back(sum/vec.size()); 32 } 33 return result; 34 } 35 };
429.N叉树的层序遍历
1 /* 2 // Definition for a Node. 3 class Node { 4 public: 5 int val; 6 vector<Node*> children; 7 8 Node() {} 9 10 Node(int _val) { 11 val = _val; 12 } 13 14 Node(int _val, vector<Node*> _children) { 15 val = _val; 16 children = _children; 17 } 18 }; 19 */ 20 21 class Solution { 22 public: 23 vector<vector<int>> levelOrder(Node* root) { 24 queue<Node*>que; 25 vector<vector<int>> result; 26 if(root!=NULL) que.push(root); 27 while(!que.empty()) 28 { 29 int size=que.size(); 30 vector<int> res;//写到for循环外边 31 for(int i=0;i<size;i++) 32 { 33 34 Node* cur=que.front(); 35 que.pop(); 36 res.push_back(cur->val); 37 if(!cur->children.empty()) 38 { 39 vector<Node*> tmp=cur->children; 40 for(int i=0;i<tmp.size();i++) 41 { 42 que.push(tmp[i]); 43 } 44 } 45 } 46 result.push_back(res); 47 } 48 return result; 49 } 50 };
111.二叉树的最小深度
1 class Solution { 2 public: 3 int minDepth(TreeNode* root) { 4 if (root == NULL) return 0; 5 int depth = 0; 6 queue<TreeNode*> que; 7 que.push(root); 8 while(!que.empty()) { 9 int size = que.size(); 10 depth++; // 记录最小深度 11 for (int i = 0; i < size; i++) { 12 TreeNode* node = que.front(); 13 que.pop(); 14 if (node->left) que.push(node->left); 15 if (node->right) que.push(node->right); 16 if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出 17 return depth; 18 } 19 } 20 } 21 return depth; 22 } 23 };
标签:node,right,TreeNode,part01,int,que,二叉树,DAY13,left From: https://www.cnblogs.com/xzdmzrc221202/p/18330815