LeetCode之二叉树的前序遍历

1. 递归解法

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Definition for binary tree 
  3.  * struct TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode *left; 
  6.  *     TreeNode *right; 
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
  8.  * }; 
  9.  */  
  10.    
  11.   
  12.    
  13. class Solution {  
  14. public:  
  15.     vector<int> path;  
  16.     void preorder(TreeNode *root)  
  17.     {  
  18.         if (root == NULL)  
  19.             return;  
  20.         path.push_back(root->val);  
  21.         preorder(root->left);  
  22.         preorder(root->right);  
  23.     }  
  24.     vector<int> preorderTraversal(TreeNode *root) {  
  25.           
  26.         preorder(root);  
  27.           
  28.         return path;  
  29.     }  
  30. };  


2. 非递归解法一(空间复杂度O(n))

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Definition for binary tree 
  3.  * struct TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode *left; 
  6.  *     TreeNode *right; 
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
  8.  * }; 
  9.  */  
  10. class Solution {  
  11. public:  
  12.     vector<int> preorderTraversal(TreeNode *root) {  
  13.         vector<int> path;  
  14.         path.clear();  
  15.         stack<TreeNode *> st;  
  16.         if (root == NULL)  
  17.             return path;  
  18.         st.push(root);  
  19.         while (!st.empty())  
  20.         {  
  21.             TreeNode *node = st.top();  
  22.             st.pop();  
  23.             path.push_back(node->val);  
  24.             if (node->right != NULL)  
  25.                 st.push(node->right);  
  26.             if (node->left != NULL)  
  27.                 st.push(node->left);  
  28.         }  
  29.         return path;  
  30.     }  
  31. };  

3. 非递归解法(空间复杂度O(1))

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Definition for binary tree 
  3.  * struct TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode *left; 
  6.  *     TreeNode *right; 
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
  8.  * }; 
  9.  */  
  10. class Solution {  
  11. public:  
  12.     vector<int> preorderTraversal(TreeNode *root) {  
  13.         vector<int> path;  
  14.         if (root == NULL)  
  15.             return path;  
  16.               
  17.         TreeNode *cur = root;  
  18.         TreeNode *pre = NULL;  
  19.           
  20.         while (cur != NULL)  
  21.         {  
  22.             if (cur->left == NULL)  
  23.             {  
  24.                 path.push_back(cur->val);  
  25.                 cur = cur->right;  
  26.             }  
  27.             else  
  28.             {  
  29.                 pre = cur->left;  
  30.                 while (pre->right != NULL && pre->right != cur)  
  31.                     pre = pre->right;  
  32.                 if (pre->right == NULL)  
  33.                 {  
  34.                     pre->right = cur;  
  35.                     path.push_back(cur->val);  
  36.                     cur = cur->left;  
  37.                 }  
  38.                 else  
  39.                 {  
  40.                     pre->right = NULL;  
  41.                     cur = cur->right;  
  42.                 }  
  43.             }  
  44.         }  
  45.         return path;  
  46.     }  
  47. };  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值