标签:后序 turn def node AC solution bin 二叉树的遍历 span
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; } };
标签:后序 turn def node AC solution bin 二叉树的遍历 span
原文地址:https://www.cnblogs.com/eilearn/p/9217393.html