两种写法,递归和非递归写法
递归:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> res;void travel(TreeNode* root){if(root == NULL){return;}res.push_back(root->val);travel(root->left) ;travel(root->right) ;}vector<int> preorderTraversal(TreeNode* root) {travel(root);return res;}
};
非递归:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> stackOrder;if(root == NULL){return res;}stackOrder.push(root);while(!stackOrder.empty()){TreeNode * node = stackOrder.top();stackOrder.pop();if(node != NULL){if(node->right != NULL){stackOrder.push(node->right);}if(node->left != NULL){stackOrder.push(node->left);}stackOrder.push(node);stackOrder.push(NULL);}else{TreeNode * cur = stackOrder.top();stackOrder.pop();res.push_back(cur->val);}}return res;}
};
后序遍历:
递归:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> res;void travel(TreeNode* root){if(root == NULL){return;}travel(root->left);travel(root->right);res.push_back(root->val);}vector<int> postorderTraversal(TreeNode* root) {travel(root);return res;}
};
非递归:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> stackOrder;if(root == NULL){return res;}stackOrder.push(root);while(!stackOrder.empty()){TreeNode * node = stackOrder.top();stackOrder.pop();if(node != NULL){stackOrder.push(node);stackOrder.push(NULL);if(node->right){stackOrder.push(node->right);}if(node->left){stackOrder.push(node->left);}}else{TreeNode* cur = stackOrder.top();stackOrder.pop();res.push_back(cur->val);}}return res;}
};
递归:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> res;void travel(TreeNode* root){if(root == NULL){return;}travel(root->left);res.push_back(root->val);travel(root->right);}vector<int> inorderTraversal(TreeNode* root) {travel(root);return res;}
};
非递归:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> stackOrder;if(root == NULL){return res;}stackOrder.push(root);while(!stackOrder.empty()){TreeNode * node = stackOrder.top();stackOrder.pop();if(node!= NULL){if(node->right != NULL){stackOrder.push(node->right);}stackOrder.push(node);stackOrder.push(NULL);if(node->left != NULL){stackOrder.push(node->left);}}else{TreeNode * cur = stackOrder.top();stackOrder.pop();res.push_back(cur->val);}}return res;}
};
用栈来记录访问的元素,同时不断调整栈顶元素所在子树的操作次序,需要进行操作的节点前加入空节点NULL