//层次遍历 栈的应用
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if (!root) {
return res;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()){
int currentLevelSize = q.size();
res.push_back(vector<int>());
for (int i = 1; i <= currentLevelSize; i++) {
auto node = q.front();
q.pop();
res.back().push_back(node->val);
if (node->left)
q.push(node->left);
if (node->right)
q.push(node->right);
}
}
return res;
}
};
//先序遍历 递归
struct TreeNode{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
void preorder(struct TreeNode* root, int *res, int* resSize)
{
if(root == NULL)
{
return;
}
res[*resSieze++] = root->val;
preorder(root->left,res,resSize);
preorder(root->right,res,resSize);
}
int preorderTraversal(struct TreeNode* root,int* returnSize){
int *res = malloc(sizeof(int)*200);
*returnSize=0;
preorder(root,res,returnSize);
return res;
}
//迭代法
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if (root == NULL) {
return res;
}
stack<TreeNode*> stk;
TreeNode* node = root;
while (!stk.empty() || node != NULL) {
//此循环完成头节点和左节点的值插入
while (node != NULL) {
//根节点的值插入
res.emplace_back(node->val);
//将链表头节点插入栈
stk.emplace(node);
//将节点换到下一个左节点(可能是下一个头节点)
node = node->left;
}
node = stk.top();
stk.pop();
//有右节点会执行第一个循环(又是头节点)
node = node->right;
}
return res;
}
};
//中序遍历
void inorder(struct TreeNode* root,int* res,int* returnSize){
if(root==NULL){
return;
}
//先左再根
inorder(root->left,res,returnSize);
res[(*returnSize)++] = root->val;
inorder(root->right,res,returnSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int* res = malloc(sizeof(int)*100);
*returnSize=0;
inorder(root,res,returnSize);
return res;
}
//迭代法c
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
*returnSize = 0;
int* res = malloc(sizeof(int) * 501);
struct TreeNode** stk = malloc(sizeof(struct TreeNode*) * 501);
int top = 0;
while (root != NULL || top > 0) {
while (root != NULL) {
//先后入栈根和根左节点的所有的根,左节点
stk[top++] = root;
root = root->left;
}
//出左节点
root = stk[--top];
//赋值左节点和根节点
res[(*returnSize)++] = root->val;
//最左边的点没有左节点时会找右节点,没找到root=NULL
root = root->right;
}
return res;
}
//后序遍历
//递归
void inorder(TreeNode* root,int* res,int*returnSize){
if(root==NULL)
return;
inorder(root->left,res,returnSize);
inorder(root->right,res,returnSize);
res[(*returnSize)++] = root->val;
}
void inorderTraversal(TreeNode* root,int*returnSize){
int* res = malloc(sizeof(int)*200);
*returnSize=0;
inorder(root,res,returnSize);
return res;
}
//迭代法c++
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
TreeNode* prev = nullptr;
vector<int> res;
if (root == nulltr) {
return res;
}
stack<TreeNode*> stk;
TreeNode* node = root;
while (!stk.empty() || node != nullptr) {
while (node != nullptr) {
stk.emplace(node);
node = node->left;
}
//第一次最左节点,
node = stk.top();
stk.pop();
if(node->right==nullptr||node->right==prev){
res.emplace_back(node->val);
prev=node;
node=nullptr;
}else{
//再次入根节点
stk.emplace(node);
node=node->right;
}
}
return res;
}
};