【leetcode】二叉树的前/中/后序 遍历

230 篇文章 0 订阅
4 篇文章 0 订阅

前序遍历

144. Binary Tree Preorder Traversal

Medium

Given a binary tree, return the preorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3
Output: [1,2,3]

Follow up: Recursive solution is trivial, could you do it iteratively?

题目链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/

法一:递归

注意:递归子节点就是在循环,千万别再加while了。。

/**
 * 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) {
        if (root==NULL) return vector<int>();
        vector<int> res;
        res.push_back(root->val);
        vector<int> l = preorderTraversal(root->left);
        if(!l.empty()){
            res.insert(res.end(), l.begin(), l.end());
        }
        vector<int> r = preorderTraversal(root->right);
        if(!r.empty()){
            res.insert(res.end(), r.begin(), r.end());
        }
        return res;
    }
};

法二:非递归

用栈实现:将需要操作的节点压入栈内,逐个访问栈内元素,直到栈空。

/**
 * 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) {
        if (root==NULL) return vector<int>();
        vector<int> res;
        stack<TreeNode*> record;
        record.push(root);
        while(!record.empty()){
            auto tmp = record.top();
            record.pop();
            if(tmp){
                res.push_back(tmp->val);
                record.push(tmp->right);
                record.push(tmp->left);
            }
        }
        return res;
    }
};

 

中序遍历

94. Binary Tree Inorder Traversal

Medium

Given a binary tree, return the inorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3
Output: [1,3,2]

Follow up: Recursive solution is trivial, could you do it iteratively?

题目链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/

法一:递归

/**
 * 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) {
        vector<int> res;
        if(root==NULL) return res;
        vector<int> l = inorderTraversal(root->left);
        if(!l.empty()) res.insert(res.end(), l.begin(), l.end());
        res.push_back(root->val);
        vector<int> r = inorderTraversal(root->right);
        if(!r.empty()) res.insert(res.end(), r.begin(), r.end());
        return res;
    }
};

法二:非递归,栈

/**
 * 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) {
        vector<int> head;
        stack<TreeNode*> pred;
        TreeNode* curr = root;
        while(curr != NULL || !pred.empty()){
            if(curr != NULL){
                pred.push(curr);
                curr = curr -> left;
            }else{
                curr = pred.top();
                pred.pop();
                head.push_back(curr->val);
                curr = curr -> right;
            }
        }
        return head;
    }
};

法三:非递归,前驱后继

/**
 * 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){
    vector<int> head;
    TreeNode* cur;
    cur = root;
    while (cur != NULL) {
        if (cur->left == NULL) {
            head.push_back(cur->val);
            cur = cur->right;   /* 将右孩子作为当前节点 */
        }
        else {
            /* 查找cur节点的前驱节点 */
            TreeNode *node = cur->left;
            while (node->right != NULL && node->right != cur)
                node = node->right;

            if (node->right == NULL) { /* 还没有线索化,则建立线索 */
                node->right = cur;
                cur = cur->left;
            }
            else { /* 如果已经线索化了,说明从子节点回到cur,则访问cur,并删除线索 */
                head.push_back(cur->val);
                node->right = NULL;
                cur = cur->right;
            }
        }
    }
    return head;
}
};

 

后序遍历

145. Binary Tree Postorder Traversal

Hard

Given a binary tree, return the postorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3
Output: [3,2,1]

Follow up: Recursive solution is trivial, could you do it iteratively?

题目链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/

法一:递归

/**
 * 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;
        vector<int> l = postorderTraversal(root->left);
        if(!l.empty()) res.insert(res.end(), l.begin(), l.end());
        vector<int> r = postorderTraversal(root->right);
        if(!r.empty()) res.insert(res.end(),r.begin(),r.end());
        res.push_back(root->val);
        return res;
    }
    void print(vector<int> v){
        for(auto i: v){
            cout<<i<<" ";
        }
        cout<<endl;
    }
};

法二:非递归,栈,vector反着插入

/**
 * 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<TreeNode*> record;
        record.push(root);
        while(!record.empty()){
            auto tmp = record.top();
            record.pop();
            if(tmp){
                res.insert(res.begin(),tmp->val);
                record.push(tmp->left);
                record.push(tmp->right);
            }
        }
        return res;
    }
};

法三:非递归,栈,前驱节点

将左子节点不断压栈直到遇到null。

取栈顶节点,看其是否存在右子节点,有则不出栈,重复压左子的操作;没有则弹出栈并输出。

/**
 * 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<TreeNode*> record;
        auto cur = root;
        TreeNode* pre = NULL;
        while(cur || !record.empty()){
            while(cur){
                record.push(cur);
                cur = cur->left;
            }
            cur = record.top();
            if(cur->right==NULL || pre==cur->right){
                record.pop();
                res.push_back(cur->val);
                pre = cur;
                cur = NULL;
            }else{
                cur = cur->right;
            }
        }
        return res;
    }
};

法四:非递归,指令栈

模拟编译器的方法:将输出、访问分为不同指令,压入栈中,再逐一弹出完成。

法五:非递归,前驱后继节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值