层序遍历(宽度优先遍历)(从上到下):使用双端队列deque
- /**
- * Definition of TreeNode:
- * class TreeNode {
- * public:
- * int val;
- * TreeNode *left, *right;
- * TreeNode(int val) {
- * this->val = val;
- * this->left = this->right = NULL;
- * }
- * }
- */
- class Solution {
- public:
- /**
- * @param root: A Tree
- * @return: Level order a list of lists of integer
- */
- vector<vector<int>> levelOrder(TreeNode * root) {
- // write your code here
- vector<vector<int> >st;
- deque<TreeNode*>node;//双端队列
- if (root == NULL)
- return st;
- node.push_back(root);
- int len;
- while(!node.empty())
- {
- vector<int>temp;
- len=node.size();//很重要这一步,不能用empty()测试node是否为空
- while(len--)
- {
- TreeNode*pt=node.front();
- temp.push_back(pt->val);
- node.pop_front();
- if(pt->left)
- node.push_back(pt->left);
- if(pt->right)
- node.push_back(pt->right);
- }
- st.push_back(temp);
- }
- return st;
- }
- };
- /**
- * Definition of TreeNode:
- * class TreeNode {
- * public:
- * int val;
- * TreeNode *left, *right;
- * TreeNode(int val) {
- * this->val = val;
- * this->left = this->right = NULL;
- * }
- * }
- */
- class Solution {
- public:
- /**
- * @param root: A tree
- * @return: buttom-up level order a list of lists of integer
- */
- vector<vector<int>> levelOrderBottom(TreeNode * root) {
- // write your code here
- vector<vector<int> >st;
- deque<TreeNode*>node;//双端队列
- if (root == NULL)
- return st;
- node.push_back(root);
- int len;
- while(!node.empty())
- {
- vector<int>temp;
- len=node.size();//很重要这一步,不能用empty()测试node是否为空
- while(len--)
- {
- TreeNode*pt=node.front();
- temp.push_back(pt->val);
- node.pop_front();
- if(pt->left)
- node.push_back(pt->left);
- if(pt->right)
- node.push_back(pt->right);
- }
- st.insert(st.begin(),temp);
- }
- return st;
- }
- };
分析:第一层从左到右遍历、第二层从右到左遍历,第三层从左到右遍历.......
设置两个栈,一个保存奇数层结点(结点先右后左保存)、一个保存偶数层结点((结点先左后右保存))。
- /**
- * Definition of TreeNode:
- * class TreeNode {
- * public:
- * int val;
- * TreeNode *left, *right;
- * TreeNode(int val) {
- * this->val = val;
- * this->left = this->right = NULL;
- * }
- * }
- */
- class Solution {
- public:
- /**
- * @param root: A Tree
- * @return: A list of lists of integer include the zigzag level order traversal of its nodes' values.
- */
- vector<vector<int>> zigzagLevelOrder(TreeNode * root) {
- // write your code here
- vector<vector<int> >st;
- stack<TreeNode*>node1;
- stack<TreeNode*>node2;//使用两个栈
- if (root == NULL)
- return st;
- node1.push(root);
- int level=1;
- vector<int>temp;
- while(!node1.empty()||!node2.empty())
- {
- if(level%2==1)
- {
- TreeNode*pt=node1.top();
- temp.push_back(pt->val);
- node1.pop();
- if(pt->left)
- node2.push(pt->left);
- if(pt->right)
- node2.push(pt->right);
- if(node1.empty())
- {
- level++;
- st.push_back(temp);
- temp.clear();
- }
- }
- else
- {
- TreeNode*pt=node2.top();
- temp.push_back(pt->val);
- node2.pop();
- if(pt->right)
- node1.push(pt->right);
- if(pt->left)
- node1.push(pt->left);
- if(node2.empty())
- {
- level++;
- st.push_back(temp);
- temp.clear();
- }
- }
- }
- return st;
- }
- };
层序遍历的思路,但是对偶数层的temp使用reverse函数
- /**
- * Definition of TreeNode:
- * class TreeNode {
- * public:
- * int val;
- * TreeNode *left, *right;
- * TreeNode(int val) {
- * this->val = val;
- * this->left = this->right = NULL;
- * }
- * }
- */
- class Solution {
- public:
- /**
- * @param root: A Tree
- * @return: A list of lists of integer include the zigzag level order traversal of its nodes' values.
- */
- vector<vector<int>> zigzagLevelOrder(TreeNode * root) {
- // write your code here
- vector<vector<int> >st;
- deque<TreeNode*>node;
- if (root == NULL)
- return st;
- node.push_back(root);
- bool count=true;
- vector<int>temp;
- int len;
- while(!node.empty())
- {
- len=node.size();
- while(len--)
- {
- TreeNode*pt=node.front();
- temp.push_back(pt->val);
- node.pop_front();
- if(pt->left)
- node.push_back(pt->left);
- if(pt->right)
- node.push_back(pt->right);
- }
- if(!count)
- reverse(temp.begin(),temp.end());
- //对temp使用reverse函数,一种比较简便的方法
- count=!count;
- st.push_back(temp);
- temp.clear();
- }
- return st;
- }
- };