二叉树前序,中序,后序遍历方法

二叉树的遍历,前序,中序,后序。指的是根结点在遍历顺序上的位置。

1.前序遍历

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

示例:

输入: [1,null,2,3]
1

2
/
3

输出: [1,2,3]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

1.1 用递归的方法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    vector<int> preorderTraversal(TreeNode* root) {
        //先写递归的方法
        if(root==NULL)
            return res;
        res.push_back(root->val);
        preorderTraversal(root->left);
        preorderTraversal(root->right);
        return res;
    }
};

1.2 用栈

方法1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        //用栈
        stack<TreeNode*> st;
        vector<int>  res;
        TreeNode* cur;
        cur=root;
        while(cur!=NULL||!st.empty())
        {
            while(cur!=NULL)
            {          
                st.push(cur);
                res.push_back(cur->val);
                cur=cur->left;
            }
            cur=st.top();
            st.pop();
            cur=cur->right;
        }
        return res;
    }
};

方法2

和方法1方法差不多,是末端结点判断不同

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root == NULL)
            return res;

        stack<TreeNode*> stack;
        TreeNode* cur = root;
        while(cur != NULL || !stack.empty()){
            if(cur != NULL){
                res.push_back(cur->val);
                stack.push(cur);
                cur = cur->left;
            }
            else{
                cur = stack.top(); //转向右结点
                stack.pop();
                cur = cur->right;
            }
        }
        return res;
    }
};

方法3

和之前的方法不同,先放入右结点,在放入左节点。这个就有点模仿递归的方法了

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;
        if(root==NULL)
            return res;
        st.push(root);
        while(!st.empty())
        {
            TreeNode* cur = st.top();
            res.push_back(cur->val);
            st.pop();

            if(cur->right!=NULL)  //先放入右结点,再放入左节点
                st.push(cur->right);
            if(cur->left!=NULL)
                st.push(cur->left);
                   
        }
        return res;
    }
};

2. 中序遍历

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

示例:

输入: [1,null,2,3]
1

2
/
3

输出: [1,3,2]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2.1 递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    vector<int> inorderTraversal(TreeNode* root) {
        //先用递归
        if(root==NULL)
            return res;
        inorderTraversal(root->left);
        res.push_back(root->val);
        inorderTraversal(root->right);
        return res;

    }
};

2.2 栈

方法一

和前序遍历不同的地方是输出道动态数组的时间

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

        stack<TreeNode*> st;
        vector<int>  res;
        if(root==NULL)
            return res;
        TreeNode* cur;
        cur = root;
        while(cur!=NULL||!st.empty())
        {
            while(cur!=NULL)
            {
                st.push(cur);
                cur=cur->left;
            }
            cur = st.top();
            st.pop();
            res.push_back(cur->val);
            cur=cur->right;
        }
        
    }
};

方法2

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

        stack<TreeNode*> st;
        vector<int>  res;
        if(root==NULL)
            return res;
        TreeNode* cur;
        cur = root;
        while(cur!=NULL||!st.empty())
        {
            if(cur!=NULL)
            {
                st.push(cur);
                cur=cur->left;
            }
            else
            {
                cur = st.top();
                st.pop();
                res.push_back(cur->val);
                cur=cur->right;
            }
        }

        return res;
    }            
};

3.后序遍历

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

示例:

输入: [1,null,2,3]
1

2
/
3

输出: [3,2,1]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

3.1 递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    vector<int> postorderTraversal(TreeNode* root) {
        //用递归的方法
        if(root==NULL)
            return res;
        postorderTraversal(root->left);
        postorderTraversal(root->right);
        res.push_back(root->val);
        return res;
    }
};

3.2栈

后序遍历用栈有很多种方法。

方法1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
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 com=stack.top();
            stack.pop();

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

    }

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

};

方法2

反转前序遍历的方法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        //用栈的方法
        //后序遍历的顺序 左-右-根
        //用前序遍历,然后反过来 根-右-左
        vector<int> res;
        vector<int> ans;
        if(root==NULL)
            return res;
        stack<TreeNode*> st;
        TreeNode* cur;
        cur = root;
        while(cur!=NULL||!st.empty())
        {
            while(cur!=NULL)
            {
                res.push_back(cur->val);
                st.push(cur);
                cur=cur->right;
            }
            cur=st.top();
            st.pop();
            cur = cur->left;
        }
        for(int i = res.size()-1; i >=0 ; i--)
        {
            ans.push_back(res[i]);
        }

        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值