C++二叉树遍历递归与栈循环的实现

记忆法:if 的位置
先序遍历:if  while
中序遍历:while if else
后序遍历:do while while if else break while

// BinaryTreeTraversal.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"
#include <vector>
#include <stack>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {
    }
};

class Solution {
public:
    // Recursive Solution
    static void preorderTraversalRecursively(TreeNode *root) {
        if (root) {
            printf("%d, ", root->val);
            preorderTraversalRecursively(root->left);
            preorderTraversalRecursively(root->right);
        }
    }
    static void inorderTraversalRecursively(TreeNode *root) {
        if (root) {
            inorderTraversalRecursively(root->left);
            printf("%d, ", root->val);
            inorderTraversalRecursively(root->right);
        }
    }
    static void postorderTraversalRecursively(TreeNode *root) {
        if (root) {
            postorderTraversalRecursively(root->left);
            postorderTraversalRecursively(root->right);
            printf("%d, ", root->val);
        }
    }
    // Iterative Solution
    static vector<int> preorderTraversalIteratively(TreeNode *root) {
        vector<int> result;
        stack<TreeNode *> s;
        if (root != nullptr) s.push(root);
        while (!s.empty()) {
            root = s.top();
            s.pop();
            result.push_back(root->val);
            if (root->right != nullptr) s.push(root->right);
            if (root->left != nullptr) s.push(root->left);
        }
        return result;
    }
    static vector<int> inorderTraversalIteratively(TreeNode *root) {
        vector<int> result;
        const TreeNode *p = root;
        stack<const TreeNode *> s;
        while (!s.empty() || p != nullptr) {
            if (p != nullptr) {
                s.push(p);
                p = p->left;
            } else {
                p = s.top();
                s.pop();
                result.push_back(p->val);
                p = p->right;
            }
        }
        return result;
    }
    static vector<int> postorderTraversalIteratively(TreeNode *root) {
        vector<int> result;
        const TreeNode *p, *q;
        stack<const TreeNode *> s;
        p = root;
        do {
            while (p != nullptr) {
                s.push(p);
                p = p->left;
            }
            q = nullptr;
            while (!s.empty()) {
                p = s.top();
                s.pop();
                if (p->right != q) {
                    s.push(p);
                    p = p->right;
                    break;
                } else {
                    result.push_back(p->val);
                    q = p;
                }
            }
        } while (!s.empty());
        return result;
    }
};

int _tmain(int argc, _TCHAR* argv[]) {
    TreeNode * root = NULL;
    root = new TreeNode(0);
    root->left = new TreeNode(1);
    root->right = new TreeNode(2);
    root->left->left = new TreeNode(3);
    root->left->right = new TreeNode(4);
    root->right->left = new TreeNode(5);
    root->right->right = new TreeNode(6);
    Solution::preorderTraversalRecursively(root); printf("\n");
    Solution::inorderTraversalRecursively(root); printf("\n");
    Solution::postorderTraversalRecursively(root); printf("\n");
    vector<int> result = Solution::preorderTraversalIteratively(root);
    for (int i : result) {
        printf("%d, ", i);
    }
    printf("\n");
    result = Solution::inorderTraversalIteratively(root);
    for (int i : result) {
        printf("%d, ", i);
    }
    printf("\n");
    result = Solution::postorderTraversalIteratively(root);
    for (int i : result) {
        printf("%d, ", i);
    }
    printf("\n");
    system("pause");
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值