zl程序教程

您现在的位置是:首页 >  其他

当前栏目

leetcode 144 145 94二叉树的三种非递归遍历

LeetCode二叉树遍历递归 三种 94 144 145
2023-09-27 14:29:24 时间

leetcode144 非递归前序遍历

使用栈来模拟递归。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> my_stack; 

        if(root == nullptr) return result;

        my_stack.push(root);//前序遍历是中左右,先处理一个中就是root

        while(my_stack.empty() != 1)
        {
            TreeNode* my_node = my_stack.top();//提前中节点
            my_stack.pop();
            //中节点压入结果
            result.push_back(my_node->val);
			//之后将中节点的左右子节点放到栈里,作为未来的中节点
			//压入栈的顺序和弹出栈是相反的,先遍历左再是右,所有先压入右再压入左
            if(my_node->right != nullptr) my_stack.push(my_node->right);
            if(my_node->left  != nullptr) my_stack.push(my_node->left);
        }

        return result;
    }
};

leetcode144 非递归后序遍历

使用栈来模拟递归。
核心逻辑和前序遍历一样,前序遍历是中左右,后序是中右左。
和前序代码类似,改变顺序。变成中右左。然后反转变成左右中

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> my_stack;

        if(root == nullptr) return result;

        my_stack.push(root);

        while(my_stack.empty() != 1)
        {
            TreeNode* my_node = my_stack.top();
            my_stack.pop();
            //和前序一样,但是变成中右左
            result.push_back(my_node->val);
            if(my_node->left != nullptr) my_stack.push(my_node->left);
            if(my_node->right != nullptr) my_stack.push(my_node->right);  
        }
		//反转,变成左右中
        reverse (result.begin() , result.end());
        return result;

    }
};

leetcode94 非递归中序遍历

和前序后序的思路不一样。中序是左中右
先找到最左的叶子节点,然后开始输出。
输出左边的之后,从栈弹出一个,弹出的是输出左节点的中节点。
然后输出中节点,再到右节点。
找右节点有没有左子叶,不停循环

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        
        vector<int> result;
        stack<TreeNode*> my_stack;

        if(root == nullptr) return result;
        TreeNode* cur = root;

        while(cur != nullptr || my_stack.empty() != 1)
        {
            if(cur != nullptr)//找到cur的最左叶子节点
            {
                my_stack.push(cur);//找的过程中所有的左节点都存起来
                cur = cur->left;

            }else//处理中节点和右节点
            {
                cur = my_stack.top();//输出栈里之前存的左节点 ,这时左节点看作成中间节点
                my_stack.pop();

                result.push_back(cur->val);
                cur = cur->right;//然后找刚才输出左节点作为中间点时的右节点
            }

        }       
        return result;

    }
};