代码题(6)― 二叉树的遍历

匿名 (未验证) 提交于 2019-12-03 00:33:02

1、144. 二叉树的前序遍历

(1)递归

/**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}  * };  */ class Solution { public:     vector<int> res;     vector<int> preorderTraversal(TreeNode* root) {         if(root == nullptr)             return res;         res.push_back(root->val);         preorderTraversal(root->left);         preorderTraversal(root->right);         return res;     } };

 

(2)非递归

/**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}  * };  */ class Solution { public:          vector<int> preorderTraversal(TreeNode* root) {         vector<int> res;         if(root == nullptr)             return res;         stack<TreeNode*> stNode;         while(!stNode.empty() || root)         {             while(root)             {                 stNode.push(root);                 res.push_back(root->val);                 root = root->left;             }             root = stNode.top();             stNode.pop();             root = root->right;         }         return res;     } };

 

 

2、94. 二叉树的中序遍历

 

(1)递归

 

/**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}  * };  */ class Solution { public:     vector<int> res;     vector<int> inorderTraversal(TreeNode* root) {         if(root != nullptr)         {             inorderTraversal(root->left);             res.push_back(root->val);             inorderTraversal(root->right);         }         return res;     } };

 

(2)非递归

 

/**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}  * };  */ class Solution { public:     vector<int> inorderTraversal(TreeNode* root) {         vector<int> res;         if(root == nullptr)             return res;         stack<TreeNode*> stNode;         while(!stNode.empty() || root)         {             while(root)             {                 stNode.push(root);                 root = root->left;             }             root = stNode.top();             stNode.pop();             res.push_back(root->val);             root = root->right;         }         return res;     } };

 

3、145. 二叉树的后序遍历

(1)递归

 

/**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}  * };  */ class Solution { public:     vector<int> res;     vector<int> postorderTraversal(TreeNode* root) {         if(root == nullptr)             return res;         postorderTraversal(root->left);         postorderTraversal(root->right);           res.push_back(root->val);         return res;     } };

 

(2)非递归

 

/**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}  * };  */ class Solution { public:     vector<int> postorderTraversal(TreeNode* root) {         vector<int> res;         if(root == nullptr)             return res;         stack<TreeNode*> stNode;         TreeNode* pre = nullptr;         while(!stNode.empty() || root)         {             while(root)             {                 stNode.push(root);                 root = root->left;             }             TreeNode* cur = stNode.top();             if(cur->right == nullptr || pre == cur->right)  //如果没有右节点,或者右节点被访问过,则可以访问该节点             {                 res.push_back(cur->val);                 stNode.pop();                 pre = cur;             }             else                 root = cur->right;         }         return res;     } };

 

原文:https://www.cnblogs.com/eilearn/p/9217393.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!