二叉树的遍历
先序遍历非递归
算法1
class Solution { public: vector<int> preorderTraversal(TreeNode* root) { stack<TreeNode*> st; vector<int> result; if (root == NULL) return result; st.push(root); while (!st.empty()) { TreeNode* node = st.top(); // 中 st.pop(); result.push_back(node->val); if (node->right) st.push(node->right); // 右(空节点不入栈) if (node->left) st.push(node->left); // 左(空节点不入栈) } return result; } };
先序遍历非递归算法2
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;
        TreeNode*p=root;
        while(p!=nullptr||!st.empty())
        {
            if(p!=nullptr)
            {
                res.push_back(p->val);
                st.push(p);
                p=p->left;
            }else{
                p=st.top();
                st.pop();
                p=p->right;
            }
        }
        return res;
    }
};
后序遍历非递归算法带r
void PostOrder(BiTree T){ stack<char> S; BitNode*p=T; BiTNode*r=NULL; while(p||!S.empty()){ //走到最左边 if(p){ S.push(p); p=p->lchild; }else{//向右 p=S.top();//下一步要分清左子树返回的还是右子树返回的 if(p->rchild&&p->rchild!=r)//如果右孩子存在,并且没有访问过 p=p->rchild; else{ S.pop(); p=S.top(); visit(p->data); r=p;//记录访问过的结点 p=NULL; } } } }
后序遍历非递归算法tag
二叉链表类型定义: typedef struct BiTNode { TElemType data; struct BiTNode *lchild,*rchild; } BiTNode, *BiTree; 可用栈类型Stack的相关定义: typedef struct { struct BiTNode *ptr; // 二叉树结点的指针类型 int tag; // 0..1 } SElemType; // 栈的元素类型 void PostOrder(BiTree T, void (*visit)(TElemType)) /* 使用栈,非递归后序遍历二叉树T, */ /* 对每个结点的元素域data调用函数visit */ { Stack S; SElemType e; InitStack(S); while(!StackEmpty(S) || T != NULL){
//向左走到底 while(T != NULL){ e.ptr = T; e.tag = 0; Push(S,e); T = T->lchild; }
//取栈顶元素 Pop(S,e); T = e.ptr;
//如果右子树不为空且没经过 if(e.tag == 0 && T -> rchild != NULL){ e.tag = 1; Push(S,e); T = T -> rchild; }
//不存在右子树或者右子树已被访问过
else if(e.tag == 1 || T -> rchild == NULL){ visit(T->data); T = NULL; } } }
层次遍历非递归版本
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { queue<TreeNode*> que; if (root != NULL) que.push(root); vector<vector<int>> result; while (!que.empty()) { int size = que.size(); vector<int> vec; // 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的 for (int i = 0; i < size; i++) { TreeNode* node = que.front(); que.pop(); vec.push_back(node->val); if (node->left) que.push(node->left); if (node->right) que.push(node->right); } result.push_back(vec); } return result; } };
层次遍历递归版本dfs
class Solution { public: void order(TreeNode* cur, vector<vector<int>>& result, int depth) { if (cur == nullptr) return; if (result.size() == depth) result.push_back(vector<int>()); result[depth].push_back(cur->val); order(cur->left, result, depth + 1); order(cur->right, result, depth + 1); } vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> result; int depth = 0; order(root, result, depth);//第几层就存到result[depth][]中,depth代表行 return result; } };
 根据先序和中序构造二叉树
#include<stdio.h> #include<string.h> typedef struct node{ char data; struct node *lchild,*rchild; }Tree; //前序遍历,中序遍历,结点个数 Tree* CreatTree(char before[],char middle[],int n) { if(n==0) return NULL; Tree*tree=(Tree*)malloc(sizeof(Tree)); tree->data=before[0]; int i=0; while(middle[i]!=before[0]) { i++; } tree->lchild=CreatTree(before+1,middle,i); tree->rchild=CreatTree(before+i+1,middle+i+1,n-i-1); return tree; }
 

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号