剑指offer 32 从上到下打印二叉树

层序遍历(宽度优先遍历)(从上到下):使用双端队列deque
[cpp]  view plain  copy
  1. /** 
  2.  * Definition of TreeNode: 
  3.  * class TreeNode { 
  4.  * public: 
  5.  *     int val; 
  6.  *     TreeNode *left, *right; 
  7.  *     TreeNode(int val) { 
  8.  *         this->val = val; 
  9.  *         this->left = this->right = NULL; 
  10.  *     } 
  11.  * } 
  12.  */  
  13.   
  14. class Solution {  
  15. public:  
  16.     /** 
  17.      * @param root: A Tree 
  18.      * @return: Level order a list of lists of integer 
  19.      */  
  20.     vector<vector<int>> levelOrder(TreeNode * root) {  
  21.         // write your code here  
  22.         vector<vector<int> >st;  
  23.         deque<TreeNode*>node;//双端队列  
  24.       if (root == NULL)  
  25.             return st;  
  26.           
  27.         node.push_back(root);  
  28.         int len;  
  29.         while(!node.empty())  
  30.         {       
  31.              vector<int>temp;  
  32.              len=node.size();//很重要这一步,不能用empty()测试node是否为空  
  33.             while(len--)  
  34.             {  
  35.               TreeNode*pt=node.front();  
  36.               temp.push_back(pt->val);  
  37.                
  38.               node.pop_front();  
  39.               if(pt->left)  
  40.               node.push_back(pt->left);  
  41.               if(pt->right)  
  42.               node.push_back(pt->right);  
  43.             }  
  44.              st.push_back(temp);  
  45.         }  
  46.         return st;  
  47.     }  
  48.       
  49. };  
层序遍历(宽度优先遍历)(从下到上):使用双端队列deque

[cpp]  view plain  copy
  1.  /** 
  2.  * Definition of TreeNode: 
  3.  * class TreeNode { 
  4.  * public: 
  5.  *     int val; 
  6.  *     TreeNode *left, *right; 
  7.  *     TreeNode(int val) { 
  8.  *         this->val = val; 
  9.  *         this->left = this->right = NULL; 
  10.  *     } 
  11.  * } 
  12.  */  
  13.   
  14. class Solution {  
  15. public:  
  16.     /** 
  17.      * @param root: A tree 
  18.      * @return: buttom-up level order a list of lists of integer 
  19.      */  
  20.     vector<vector<int>> levelOrderBottom(TreeNode * root) {  
  21.         // write your code here  
  22.         vector<vector<int> >st;  
  23.         deque<TreeNode*>node;//双端队列  
  24.       if (root == NULL)  
  25.             return st;  
  26.         node.push_back(root);  
  27.         int len;  
  28.         while(!node.empty())  
  29.         {       
  30.              vector<int>temp;  
  31.              len=node.size();//很重要这一步,不能用empty()测试node是否为空  
  32.              while(len--)  
  33.             {  
  34.               TreeNode*pt=node.front();  
  35.               temp.push_back(pt->val);  
  36.               node.pop_front();  
  37.               if(pt->left)  
  38.               node.push_back(pt->left);  
  39.               if(pt->right)  
  40.               node.push_back(pt->right);  
  41.             }  
  42.              st.insert(st.begin(),temp);  
  43.         }  
  44.         return st;  
  45.     }  
  46. };  
锯齿形遍历(宽度优先遍历)(迭代法、使用两个栈)
分析:第一层从左到右遍历、第二层从右到左遍历,第三层从左到右遍历.......

设置两个栈,一个保存奇数层结点(结点先右后左保存)、一个保存偶数层结点((结点先左后右保存))。

[cpp]  view plain  copy
  1. /** 
  2.  * Definition of TreeNode: 
  3.  * class TreeNode { 
  4.  * public: 
  5.  *     int val; 
  6.  *     TreeNode *left, *right; 
  7.  *     TreeNode(int val) { 
  8.  *         this->val = val; 
  9.  *         this->left = this->right = NULL; 
  10.  *     } 
  11.  * } 
  12.  */  
  13.   
  14. class Solution {  
  15. public:  
  16.     /** 
  17.      * @param root: A Tree 
  18.      * @return: A list of lists of integer include the zigzag level order traversal of its nodes' values. 
  19.      */  
  20.     vector<vector<int>> zigzagLevelOrder(TreeNode * root) {  
  21.         // write your code here  
  22.         vector<vector<int> >st;  
  23.         stack<TreeNode*>node1;  
  24.         stack<TreeNode*>node2;//使用两个栈  
  25.       if (root == NULL)  
  26.             return st;  
  27.         node1.push(root);  
  28.         int level=1;  
  29.          vector<int>temp;  
  30.         while(!node1.empty()||!node2.empty())  
  31.         {       
  32.               
  33.             if(level%2==1)  
  34.             {  
  35.                 TreeNode*pt=node1.top();  
  36.                 temp.push_back(pt->val);  
  37.                 node1.pop();  
  38.                 if(pt->left)  
  39.                 node2.push(pt->left);  
  40.                 if(pt->right)  
  41.                 node2.push(pt->right);  
  42.                 if(node1.empty())  
  43.                 {  
  44.                     level++;  
  45.                   st.push_back(temp);  
  46.                   temp.clear();  
  47.                 }  
  48.                  
  49.              }  
  50.               else   
  51.             {  
  52.               TreeNode*pt=node2.top();  
  53.               temp.push_back(pt->val);  
  54.               node2.pop();  
  55.               if(pt->right)  
  56.               node1.push(pt->right);  
  57.               if(pt->left)  
  58.               node1.push(pt->left);  
  59.                if(node2.empty())  
  60.              {  
  61.               level++;  
  62.               st.push_back(temp);  
  63.               temp.clear();  
  64.              }  
  65.              
  66.             }  
  67.          }  
  68.         return st;  
  69.     }  
  70. };  
迭代法(使用队列实现)
层序遍历的思路,但是对偶数层的temp使用reverse函数
[cpp]  view plain  copy
  1. /** 
  2.  * Definition of TreeNode: 
  3.  * class TreeNode { 
  4.  * public: 
  5.  *     int val; 
  6.  *     TreeNode *left, *right; 
  7.  *     TreeNode(int val) { 
  8.  *         this->val = val; 
  9.  *         this->left = this->right = NULL; 
  10.  *     } 
  11.  * } 
  12.  */  
  13.   
  14. class Solution {  
  15. public:  
  16.     /** 
  17.      * @param root: A Tree 
  18.      * @return: A list of lists of integer include the zigzag level order traversal of its nodes' values. 
  19.      */  
  20.     vector<vector<int>> zigzagLevelOrder(TreeNode * root) {  
  21.         // write your code here  
  22.         vector<vector<int> >st;  
  23.        deque<TreeNode*>node;  
  24.       
  25.       if (root == NULL)  
  26.             return st;  
  27.         node.push_back(root);  
  28.        bool count=true;  
  29.          vector<int>temp;  
  30.          int len;  
  31.         while(!node.empty())  
  32.         {       
  33.             len=node.size();  
  34.             while(len--)  
  35.             {  
  36.                 TreeNode*pt=node.front();  
  37.                 temp.push_back(pt->val);  
  38.                 node.pop_front();  
  39.                 if(pt->left)  
  40.                 node.push_back(pt->left);  
  41.                 if(pt->right)  
  42.                 node.push_back(pt->right);  
  43.            }  
  44.              if(!count)    
  45.              reverse(temp.begin(),temp.end());  
  46.              //对temp使用reverse函数,一种比较简便的方法  
  47.              count=!count;  
  48.            st.push_back(temp);  
  49.            temp.clear();  
  50.          }  
  51.         return st;  
  52.     }  
  53. };  

猜你喜欢

转载自blog.csdn.net/weixin_41413441/article/details/80672226