赞
踩
思路:二叉树层次遍历可以使用队列来进行遍历。
class Solution { //二叉树的层次遍历 public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> result; queue<TreeNode*> que; if (root != nullptr){ que.push(root); } while (!que.empty()) { vector<int> temp; int length = que.size(); for (int i = 0; i < length; ++i) { TreeNode* tempNode = que.front(); que.pop(); temp.push_back(tempNode->val); if (tempNode->left) { que.push(tempNode->left); } if (tempNode->right) { que.push(tempNode->right); } } result.push_back(temp); } return result; } };
思路:在上一题的基础上,把最后得到的结果直接反转即可。
class Solution { //107. 二叉树的层序遍历 II public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> result; queue<TreeNode*> que; if (root != nullptr){ que.push(root); } while (!que.empty()) { vector<int> temp; int length = que.size(); for (int i = 0; i < length; ++i) { TreeNode* tempNode = que.front(); que.pop(); temp.push_back(tempNode->val); if (tempNode->left) { que.push(tempNode->left); } if (tempNode->right) { que.push(tempNode->right); } } result.push_back(temp); } reverse(result.begin(), result.end()); return result; } };
思路:本题可以使用双向队列deque,在102题的基础上稍作修改,在队列遍历完每一层时,将队列的最后一个节点加入到结果集result。这里改成双向队列,是因为双向队列可以直接取到队列尾部的值。
class Solution { //199. 二叉树的右视图 public: vector<int> rightSideView(TreeNode* root) { vector<int> result; deque<TreeNode*> que; if (root != nullptr){ que.push_back(root); } while (!que.empty()) { result.push_back(que.back()->val); int length = que.size(); for (int i = 0; i < length; ++i) { TreeNode* tempNode = que.front(); que.pop_front(); if (tempNode->left) { que.push_back(tempNode->left); } if (tempNode->right) { que.push_back(tempNode->right); } } } return result; } };
思路:在102题的基础上,略作修改即可。
class Solution { //637. 二叉树的层平均值 public: vector<double> averageOfLevels(TreeNode* root) { vector<double> result; queue<TreeNode*> que; if (root != nullptr){ que.push(root); } while (!que.empty()) { double sum = 0; int length = que.size(); for (int i = 0; i < length; ++i) { TreeNode* tempNode = que.front(); que.pop(); sum += tempNode->val; if (tempNode->left) { que.push(tempNode->left); } if (tempNode->right) { que.push(tempNode->right); } } double average = sum / (double)length; result.push_back(average); } return result; } };
思路:N叉树的层次遍历与二叉树的层次遍历流程相同,不同的地方在于二叉树是每次将队首节点的左右孩子加入队列,而N叉树是将队首节点的所有孩子加入队列,只要遍历存孩子节点的动态数组,将其加入队列即可。
class Solution { //429. N叉树的层序遍历 public: vector<vector<int>> levelOrder(Node* root) { vector<vector<int>> result; queue<Node*> que; if (root != nullptr) { que.push(root); } while (!que.empty()) { vector<int> temp; int length = que.size(); for (int i = 0; i < length; ++i) { Node* node = que.front(); que.pop(); temp.push_back(node->val); int size = (node->children).size(); for (int j = 0; j < size; ++j) { que.push(node->children[j]); } } result.push_back(temp); } return result; } };
思路:先用队列层序遍历,然后把每一层用优先级队列存储,优先级队列的队首就是树行中的最大值。
class Solution { //515. 在每个树行中找最大值 使用队列和优先级队列 public: class myComparison { public: bool operator()(TreeNode* p1, TreeNode* p2) { return p1->val < p2->val; } }; vector<int> largestValues(TreeNode* root) { vector<int> result; queue<TreeNode*> que; if (root) { que.push(root); } while (!que.empty()) { int length = que.size(); priority_queue<TreeNode*, vector<TreeNode*>, myComparison> p_que; for (int i = 0; i < length; ++i) { TreeNode* node = que.front(); que.pop(); p_que.push(node); if (node->left) { que.push(node->left); } if (node->right) { que.push(node->right); } } result.push_back(p_que.top()->val); } return result; } }; int main() { TreeNode* p6 = new TreeNode(9); TreeNode* p5 = new TreeNode(3); TreeNode* p4 = new TreeNode(5); TreeNode* p3 = new TreeNode(2, nullptr, p6); TreeNode* p2 = new TreeNode(3, p4, p5); TreeNode* p1 = new TreeNode(1, p2, p3); Solution s; vector<int> result = s.largestValues(p1); for (int i : result) { cout << i << "\t"; } cout << endl; return 0; }
116. 填充每个节点的下一个右侧节点指针
117. 填充每个节点的下一个右侧节点指针II
思路:用队列进行层次遍历,遍历每一层时,取出队首元素并弹出,next指针指向下一个队首元素即可。
class Node { public: int val; Node* left; Node* right; Node* next; Node() : val(0), left(NULL), right(NULL), next(NULL) {} Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} Node(int _val, Node* _left, Node* _right, Node* _next) : val(_val), left(_left), right(_right), next(_next) {} }; class Solution { //116、117. 填充每个节点的下一个右侧节点指针 public: Node* connect(Node* root) { if (root == nullptr) return root; queue<Node*> que; que.push(root); while (!que.empty()) { int size = que.size(); Node* node = nullptr; for (int i = 0; i < size; ++i) { node = que.front(); que.pop(); if (i < size - 1) { node->next = que.front(); } if (node->left) { que.push(node->left); } if (node->right) { que.push(node->right); } } } return root; } }; int main() { Node* p7 = new Node(7); Node* p6 = new Node(6); Node* p5 = new Node(5); Node* p4 = new Node(4); Node* p3 = new Node(3, p6, p7, nullptr); Node* p2 = new Node(2, p4, p5, nullptr); Node* p1 = new Node(1, p2, p3, nullptr); Solution s; s.connect(p1); return 0; }
思路:用队列层次遍历时记录层数。
class Solution { //104. 二叉树的最大深度 public: int maxDepth(TreeNode* root) { int result = 0; queue<TreeNode*> que; if (root != nullptr){ que.push(root); } while (!que.empty()) { int length = que.size(); for (int i = 0; i < length; ++i) { TreeNode* tempNode = que.front(); que.pop(); if (tempNode->left) { que.push(tempNode->left); } if (tempNode->right) { que.push(tempNode->right); } } result++; } return result; } };
思路:用队列对二叉树进行层次遍历,在遍历时,记录层次,如果遇见叶子节点,就层数加1并返回记录的结果。
class Solution { //111. 二叉树的最小深度 public: int minDepth(TreeNode* root) { int result = 0; queue<TreeNode*> que; if (root != nullptr){ que.push(root); } while (!que.empty()) { int length = que.size(); for (int i = 0; i < length; ++i) { TreeNode* tempNode = que.front(); que.pop(); if (tempNode->left == nullptr&&tempNode->right == nullptr) { result++; return result; } if (tempNode->left) { que.push(tempNode->left); } if (tempNode->right) { que.push(tempNode->right); } } result++; } return result; } };
参考资料:代码随想录
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。