二叉树前序遍历
递归深度搜索方法
/**
* 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> ret;
vector<int> preorderTraversal(TreeNode* root) {
if(root){
ret.push_back(root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
return ret;
}
};
迭代深度搜索方法
/**
* 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> ret;
stack<TreeNode*> st;
TreeNode*cur=root;
while(!st.empty()||cur)
{
while(cur)
{
ret.push_back(cur->val);
st.push(cur);
cur=cur->left;
}
cur=st.top();
st.pop();
cur=cur->right;
}
return ret;
}
};
二叉树中序遍历
递归深度搜索方法
/**
* 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> ret;
vector<int> inorderTraversal(TreeNode* root) {
if(root){
inorderTraversal(root->left);
ret.push_back(root->val);
inorderTraversal(root->right);
}
return ret;
}
};
迭代深度搜索方法
/**
* 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> ret;
stack<TreeNode*> st;
TreeNode* cur=root;
while(cur||!st.empty())
{
while(cur)
{
st.push(cur);
cur=cur->left;
}
cur=st.top();
st.pop();
ret.push_back(cur->val);
cur=cur->right;
}
return ret;
}
};
二叉树后续遍历
递归深度搜索方法
/**
* 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> ret;
vector<int> postorderTraversal(TreeNode* root) {
if(root)
{
postorderTraversal(root->left);
postorderTraversal(root->right);
ret.push_back(root->val);
}
return ret;
}
};
后续遍历LeetCode标记为困难,主要是这里需要标记节点的右节点是否被访问过。如果当前节点的右节点被访问过或者没有右节点,那就可以访问当前节点了。
迭代深度搜索方法
/**
* 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> ret;
stack<TreeNode*> st;
TreeNode* cur=root;
TreeNode* pre=nullptr;
while(!st.empty()||cur)
{
while(cur)
{
st.push(cur);
cur=cur->left;
}
cur=st.top();
if(cur->right==nullptr||pre==cur->right)//右节点已经遍历
{
st.pop();
ret.push_back(cur->val);
pre=cur;
cur=nullptr;
}
else
{
cur=cur->right;
pre=nullptr;
}
}
return ret;
}
};
二叉树层续遍历
递归深度搜索方法
/**
* 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<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
pre(root,0,res);
return res;
}
void pre(TreeNode *root,int depth,vector<vector<int>> &res)
{
if(!root) return;
if(depth==res.size()) //说明需要增加一层
res.push_back(vector<int>{});
res[depth].push_back(root->val);
pre(root->left,depth+1,res);
pre(root->right,depth+1,res);
}
};
迭代广度搜索方法
/**
* 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<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> qu;
vector<vector<int>> ret;
if(!root) return ret;
qu.push(root);
while(!qu.empty())
{
int len=qu.size();//这一层的元素个数
vector<int> level;
for(int i=0;i<len;i++)
{
TreeNode* node=qu.front();
level.push_back(node->val);
qu.pop();
if(node->left) qu.push(node->left);
if(node->right) qu.push(node->right);
}
ret.push_back(level);
}
return ret;
}
};