首页 > 其他分享 >二叉树的前中后序遍历(非递归)

二叉树的前中后序遍历(非递归)

时间:2023-04-02 15:14:04浏览次数:31  
标签:遍历 TreeNode cur 后序 res 二叉树 push root sta

class TreeNode
{
public:
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode():val(NULL),left(nullptr),right(nullptr){}
    TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) 
    {
        if(root == nullptr) return res;
        TreeNode *cur = root;
        sta.push(root);
        while(!sta.empty())
        {
            cur = sta.top();
            sta.pop();
            res.push_back(cur->val);
            //先压入右 左 ,出栈才能为左 右
            if(cur->right) sta.push(cur->right);
            if(cur->left) sta.push(cur->left);
        }
        return res;
    }
    vector<int> inorderTraversal(TreeNode* root)
    {
        if(root == nullptr) return res;
        TreeNode *cur = root;
        while(cur != nullptr || !sta.empty())
        {
            if(cur != nullptr)
            {
                sta.push(cur);
                cur = cur->left;
            }
            else{
                cur = sta.top();
                sta.pop();
                res.push_back(cur->val);
                cur = cur->right;
            }
        }
        return res;
    }
    vector<int> postorderTraversal(TreeNode* root)
    {
        if(root == nullptr) return res;
        TreeNode *cur = root;
        sta.push(root);
        while(!sta.empty())
        {
            cur = sta.top();
            sta.pop();
            res.push_back(cur->val);
            if(cur->left) sta.push(cur->left);
            if(cur->right) sta.push(cur->right);
        }
        reverse(res.begin(),res.end());
        return res;
    }

private:
    stack<TreeNode*> sta;
    vector<int> res;
};

标签:遍历,TreeNode,cur,后序,res,二叉树,push,root,sta
From: https://www.cnblogs.com/lihaoxiang/p/17280501.html

相关文章

  • day14| 94.二叉树的中序遍历;144.二叉树的前序遍历;145.二叉树的后序遍历
    94.二叉树的中序遍历 思路:1.找出重复的子问题这个重复的子问题是:先遍历左子树、再取根节点、最后遍历右子树2.确定终止条件当节点为空是,返回 代码如下:#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,......
  • jQuery遍历_02
       ......
  • jQuery遍历_01
         ......
  • Python遍历时删除元素问题(附深拷贝与浅拷贝介绍)
    问题有时候,我们希望用Python遍历一个列表(或其他可迭代对象),如果其中有我们不需要的元素就把它删除并继续遍历。如以下代码段,我们本希望打印1、3,可最后却只打印了1。a=[1,2,3]foriina:ifi==2:a.remove(i)else:print(i)分析其实,之所以......
  • 代码随想录Day17-Leetcode110.平衡二叉树,257. 二叉树的所有路径,404.左叶子之和
    110.平衡二叉树题目链接:https://leetcode.cn/problems/balanced-binary-tree/一个显然但似乎不太高效的方法是:通过递归获取左右子树高度,判断差;然后递归判断左右结点;那么一个显然的改进就是后序遍历/***Definitionforabinarytreenode.*functionTreeNode(val......
  • 二叉树中和为某一值的路径
    classSolution{public:vector<vector<int>>res;vector<int>path;voiddfs(TreeNode*root,intsum,intt){t+=root->val;path.push_back(root->val);if(root->left)dfs(root-&......
  • 数据结构之二叉树
    树是一种非线性数据结构,是由n(n>=0)个有限节点组成的一个具有层次关系的集合。树的逻辑结构看起来像一棵倒挂的树,根朝上,叶子朝下。树一般是递归定义的,每一棵树都可以认为是由根和其子树构成,且各个子树不相交。树树的相关概念节点的度:一个节点含有的子树的个数称为该节点的度;叶节......
  • 二叉搜索树的后序遍历序列
    classSolution{public:booldfs(vector<int>q,intl,intr){if(l>=r)returntrue;introot=q[r];intidx=l;for(;idx<r;idx++)if(q[idx]>root)break;intt=idx;......
  • LeetCode 94 二叉树的中序遍历
    LeetCode|94.二叉树的中序遍历给定一个二叉树的根节点root,返回它的中序 遍历。示例1:输入:root=[1,null,2,3]输出:[1,3,2]示例2:输入:root=[]输出:[]示例3:输入:root=[1]输出:[1]提示:树中节点数目在范围[0,100]内-100<=Node.val<=100迭代实现:......
  • 广度优先遍历
    概述广度优先搜索的设计思想广度优先搜索以顶点u为起始点,依次访问和u有路径相通且路径长度为1、2、…的顶点。广度优先搜索的基本思想是访问顶点u,然后依次访问u的各个未被访问的邻接点v1、v2、…、vk,分别从v1、v2、…、vk出发依次访问它们未被访问的邻接点至图......