二叉树的遍历(递归与非递归、层次)

版权声明:本文为博主原创文章,转载请注明出处-- https://blog.csdn.net/qq_38790716/article/details/90110267
递归遍历

1.先序遍历:根、左、右

void Preorder(TreeNode* root) {
	if (root) {
		pre_ans.push_back(root->val);
   		if (root->left)
    		Preorder(root->left);
    	if (root->right)
    		Preorder(root->right);
	}
}

2.中序遍历:左、根、右

void Inorder(TreeNode* root) {
	if (root) {
		if (root->left);
    		Inorder(root->left);
    	in_ans.push_back(root->val);
    	if (root->right)
    		Inorder(root->right);
	}
}

3.后序遍历:左、右、根

void Endorder(TreeNode* root) {
	if (root) {
		if (root->left)
    		Endorder(root->left)
    	if (root->right)
   	 		Endorder(root->right);
    	end_ans.push_back(root->val);
	}
}

非递归遍历

1.先序遍历:根、左、右

void Preorder(TreeNode* root) {
    TreeNode* pNode = root;
    stack<TreeNode*> s;
    while (pNode || !s.empty()) {
    	//边遍历边读取节点值
        while (pNode) {
            pre_ans.push_back(pNode->val);
            s.push(pNode);
            pNode = pNode->left;
        }
        TreeNode* cur = s.top();
        s.pop();
        pNode = cur->right;
    }
}

2.中序遍历:左、根、右

void Inorder(TreeNode* root) {
    TreeNode* pNode = root;
    stack<TreeNode*> s;
    while (pNode || !s.empty()) {
    	//遍历到最左
        while (pNode) {
            s.push(pNode);
            pNode = pNode->left;
        }
        //读取值
        TreeNode* cur = s.top();
        in_ans.push_back(cur->val);
        s.pop();
		//遍历右
        pNode = cur->right;
    }
}

3.后序遍历:左、右、根

void Endorder(TreeNode* root) {
    TreeNode* pNode = root;
    TreeNode* prev = nullptr;
    stack<TreeNode*> s;
    while (pNode || !s.empty()) {
    	//遍历到最左
        while (pNode) {
            s.push(pNode);
            pNode = pNode->left;
        }
        TreeNode* cur = s.top();
        //判断如果最左节点的右子树为空或为上一个遍历的节点,则读取当前节点值,并将当前节点置为遍历的上一节点
        if (cur->right == nullptr || cur->right == prev) {
            end_ans.push_back(cur->val);
            s.pop();
            prev = cur;
        }
        //否则读取右节点
        else {
            pNode = cur->right;
        }
    }
}

层序遍历

按层进行遍历

void Levelorder(TreeNode* root) {
    queue<TreeNode*> que;
    que.push(root);
    while (!que.empty()) {
        TreeNode* cur = que.front();
        level_ans.push_back(cur->val);
        if (cur->left)
            que.push(cur->left);
        if (cur->right)
            que.push(cur->right);
        que.pop();
    }
} 

测试代码

#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<int> pre_ans, in_ans, end_ans, level_ans;
void Preorder(TreeNode* root) {
	if (root) {
		pre_ans.push_back(root->val);
   		if (root->left)
    		Preorder(root->left);
    	if (root->right)
    		Preorder(root->right);
	}
}
void Inorder(TreeNode* root) {
	if (root) {
		if (root->left);
    		Inorder(root->left);
    	in_ans.push_back(root->val);
    	if (root->right)
    		Inorder(root->right);
	}
}
void Endorder(TreeNode* root) {
	if (root) {
		if (root->left)
    		Endorder(root->left);
    	if (root->right)
   	 		Endorder(root->right);
    	end_ans.push_back(root->val);
	}
}
/*void Preorder(TreeNode* root) {
    TreeNode* pNode = root;
    stack<TreeNode*> s;
    while (pNode || !s.empty()) {
        while (pNode) {
            pre_ans.push_back(pNode->val);
            s.push(pNode);
            pNode = pNode->left;
        }
        TreeNode* cur = s.top();
        s.pop();
        pNode = cur->right;
    }
}
void Inorder(TreeNode* root) {
    TreeNode* pNode = root;
    stack<TreeNode*> s;
    while (pNode || !s.empty()) {
        while (pNode) {
            s.push(pNode);
            pNode = pNode->left;
        }
        TreeNode* cur = s.top();
        in_ans.push_back(cur->val);
        s.pop();

        pNode = cur->right;
    }
}
void Endorder(TreeNode* root) {
    TreeNode* pNode = root;
    TreeNode* prev = nullptr;
    stack<TreeNode*> s;
    while (pNode || !s.empty()) {
        while (pNode) {
            s.push(pNode);
            pNode = pNode->left;
        }
        TreeNode* cur = s.top();
        if (cur->right == nullptr || cur->right == prev) {
            end_ans.push_back(cur->val);
            s.pop();
            prev = cur;
        }
        else {
            pNode = cur->right;
        }
    }
}*/
void Levelorder(TreeNode* root) {
    queue<TreeNode*> que;
    que.push(root);
    while (!que.empty()) {
        TreeNode* cur = que.front();
        level_ans.push_back(cur->val);
        if (cur->left)
            que.push(cur->left);
        if (cur->right)
            que.push(cur->right);
        que.pop();
    }
} 
/*
    1
  2   5
3  4 6  9
*/
int main() {
    TreeNode a(1);
    TreeNode b(2);
    TreeNode c(5);
    TreeNode d(3);
    TreeNode e(4);
    TreeNode f(6);
    TreeNode g(9);
    a.left = &b;
    a.right = &c;
    b.left = &d;
    b.right = &e;
    c.left = &f;
    c.right = &g;

    Preorder(&a);
    Inorder(&a);
    Endorder(&a);
    Levelorder(&a);
    cout << "preorder tree:";
    for (int i = 0; i < pre_ans.size(); ++i)
        cout << pre_ans[i] << " ";
    cout << endl;
    cout << "inorder tree:";
    for (int i = 0; i < in_ans.size(); ++i)
        cout << in_ans[i] << " ";
    cout << endl;
    cout << "endorder tree:";
    for (int i = 0; i < pre_ans.size(); ++i)
        cout << end_ans[i] << " ";
    cout << endl;
    cout << "levelorder tree:";
    for (int i = 0; i < level_ans.size(); ++i) 
        cout << level_ans[i] << " ";
    cout << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38790716/article/details/90110267
今日推荐