[LeetCode] 144. Preorder traversal of binary tree, 94. Inorder traversal of binary tree, 145. Postorder traversal of binary tree

 Author: Xiao Lu

Column: "Leetcode"

Favorite words: The world is more beautiful because of the youth who stand up. —— "People's Daily"


 144. Preorder Traversal of Binary Trees

144. Preorder Traversal of Binary Trees

topic:

Given the root node of your binary tree  , return a preorderroot traversal  of its node values  . 

Example:

Ideas:

We can go all the way to the end of the binary tree, so that there will be a leftmost path, which we call the left sub-path, we can divide the binary tree into the left sub-path and the right subtree of the left sub-path node, etc.

We use a stack to store the left sub-way, if it reaches the end, pop out the top of the stack, and then traverse the right sub-tree at the top of the stack to cycle 

code:

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        stack<TreeNode*>st;
        vector<int>v;
        TreeNode*cur=root;
        while(cur||!st.empty())
        {
            while(cur)
            {
                st.push(cur);
                v.push_back(cur->val);
                cur=cur->left;
            }
            TreeNode*top=st.top();
            st.pop();
            cur=top->right;
        }
        return v;
    }
};

 94. Inorder traversal of a binary tree

94. Inorder traversal of a binary tree

topic:

Given the root node of a binary tree  root , return  its  inorder  traversal  

Example:

code:

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        stack<TreeNode*>st;
        vector<int>v;
        TreeNode*cur=root;
        while(cur||!st.empty())
        {
            while(cur)
            {
                st.push(cur);
                cur=cur->left;
            }
 
                TreeNode*top=st.top();
                st.pop();
                v.push_back(top->val);
                
                    cur=top->right;
        }
        return v;
    }
};

 145. Postorder Traversal of Binary Trees

 145. Postorder Traversal of Binary Trees

 topic:

Given the root node of a binary tree  root , return  the postorder traversal  of its node values .

Example:

 code:

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*>st;
        vector<int>v;
        TreeNode*cur=root;
        TreeNode*prev=nullptr;
        while(cur||!st.empty())
        {
            //左子路
            while(cur)
            {
                st.push(cur);
                cur=cur->left;
            }
            //从栈里面取到左子路的节点
            TreeNode*top=st.top();
            //这里右不存在或者右子树已经遍历了,才可以
            //prev记录上一个节点,如果prev是top的父亲,就说明右子树已经遍历了
            if(top->right==nullptr||top->right==prev)
            {
                v.push_back(top->val);
                st.pop();
                prev=top;
            }
            else
            {
                cur=top->right;

            }
        }
            return v;
        
    }
};

Guess you like

Origin blog.csdn.net/m0_69061857/article/details/131062666