前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >二叉树遍历非递归程序 -- 使用栈模拟系统栈

二叉树遍历非递归程序 -- 使用栈模拟系统栈

作者头像
宇宙之一粟
发布2020-10-26 10:39:23
2890
发布2020-10-26 10:39:23
举报
文章被收录于专栏:宇宙之_一粟

一、前序遍历

给定一个二叉树,返回它的 前序 遍历。

示例:

代码语言:javascript
复制
输入: [1,null,2,3]  
   1
    \
     2
    /
   3 

输出: [1,2,3]

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

前序遍历C++代码
代码语言:javascript
复制
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

struct Command
{
    string s; // go, print
    TreeNode* node;
    Command(string s, TreeNode* node): s(s), node(node){}
};


class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        if (root == NULL)
        {
            return res;
        }
        
        stack<Command> stack;
        stack.push(Command("go", root));
        while ( !stack.empty() )
        {
            Command command = stack.top();
            stack.pop();

            if (command.s == "print")
            {
                res.push_back(command.node -> val);
            }
            else
            {
                assert( command.s == "go");
                if (command.node -> right)
                {
                    stack.push(Command("go", command.node -> right));
                }
                if (command.node -> left)
                {
                    stack.push(Command("go", command.node -> left));
                }
                stack.push(Command("print", command.node));
            } 
        }
        return res;
    }
};

二、中序遍历

给定一个二叉树,返回它的中序 遍历。

示例:

代码语言:javascript
复制
输入: [1,null,2,3]
   1
    \
     2
    /
   3

输出: [1,3,2]

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

中序遍历C++代码
代码语言:javascript
复制
// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
struct Command
{
    string s; // go, print
    TreeNode *node;
    Command(string s, TreeNode *node) : s(s), node(node) {}
};

class Solution
{
public:
    vector<int> inorderTraversal(TreeNode *root)
    {
        vector<int> res;
        if (root == NULL)
        {
            return res;
        }

        stack<Command> stack;
        stack.push(Command("go", root));
        while (!stack.empty())
        {
            Command command = stack.top();
            stack.pop();

            if (command.s == "print")
            {
                res.push_back(command.node->val);
            }
            else
            {
                assert(command.s == "go");
                
                if (command.node->right)
                {
                    stack.push(Command("go", command.node->right));
                }
                stack.push(Command("print", command.node));
                if (command.node->left)
                {
                    stack.push(Command("go", command.node->left));
                }
            }
        }
        return res;
    }
};
// @lc code=end

三、后序遍历

给定一个二叉树,返回它的 后序 遍历。

示例:

代码语言:javascript
复制
输入: [1,null,2,3]  
   1
    \
     2
    /
   3 

输出: [3,2,1]

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

后序遍历C++代码
代码语言:javascript
复制
// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
struct Command
{
    string s; // go, print
    TreeNode *node;
    Command(string s, TreeNode *node) : s(s), node(node) {}
};

class Solution
{
public:
    vector<int> postorderTraversal(TreeNode *root)
    {
        vector<int> res;
        if (root == NULL)
        {
            return res;
        }

        stack<Command> stack;
        stack.push(Command("go", root));
        while (!stack.empty())
        {
            Command command = stack.top();
            stack.pop();

            if (command.s == "print")
            {
                res.push_back(command.node->val);
            }
            else
            {
                assert(command.s == "go");
                stack.push(Command("print", command.node));
                if (command.node->right)
                {
                    stack.push(Command("go", command.node->right));
                }
                if (command.node->left)
                {
                    stack.push(Command("go", command.node->left));
                }
            }
        }
        return res;
    }
};
// @lc code=end

练习题: 341.Flatten Nested List Iterator

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前序遍历
    • 前序遍历C++代码
    • 二、中序遍历
      • 中序遍历C++代码
      • 三、后序遍历
        • 后序遍历C++代码
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档