今日任務
二叉樹的遞迴遍歷(前中後)
二叉樹的迭代遍歷(前中後)
二叉樹的統一迭代遍歷
二叉樹的層序遍歷(共十道題目)
完成情況
遞迴已掌握,程式碼略
迭代前中手寫一編,後和統一未學習
層序遍歷題目如下
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 };