LeetCode 二叉樹的中序遍歷 (最詳細的解法!!!)

給定一個二叉樹,返回它的中序 遍歷。

示例:

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

2
/
3

輸出: [1,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> inorderTraversal(TreeNode *root) {
         vector<int> res;
         inorder(root, res);
         return res;
     }
     void inorder(TreeNode *root, vector<int> &res) {
         if (!root) 
             return;
         if (root->left) 
             inorder(root->left, res);
         res.push_back(root->val);
         if (root->right) 
             inorder(root->right, res);
     }
 };

代碼2

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        if(root){
            inorderTraversal(root->left);
            res.push_back(root->val);
            inorderTraversal(root->right);
        }
        return res;
    }
private:
        vector<int> res;
};

迭代的解題思路1:
從根節點開始,先將根節點壓入棧,然後再將其所有左子結點壓入棧,然後取出棧頂節點,保存節點值,再將當前指針移到其右子節點上,若存在右子節點,則在下次循環時又可將其所有左子結點壓入棧中。這樣就保證了訪問順序爲左-根-右。

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> s;
        TreeNode *p = root;
        while (!s.empty() || p) {
            if (p) {
                s.push(p);
                p = p->left;
            } else {
                TreeNode *t = s.top(); s.pop();
                res.push_back(t->val);
                p = t->right;
            }
        }
        return res;
    }
};

迭代的解題思路2:
個人覺得沒有思路一好。

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> s;
        TreeNode *p = root;
        while (!s.empty() || p) {
            if (p) {
                s.push(p);
                p = p->left;
            } 
            else {
                TreeNode *t = s.top(); 
                s.pop();
                res.push_back(t->val);
                p = t->right;
            }
        }
        return res;
    }
};

迭代的解題思路3:
使用Threaded binary tree,算法如下:1. 初始化指針cur指向root。2. 當cur不爲空時,如果cur沒有左子結點,a) 打印出cur的值。 b) 將cur指針指向其右子節點。反之,將pre指針指向cur的左子樹中的最右子節點,若pre不存在右子節點,a) 將其右子節點指回cur。b) cur指向其左子節點。反之,a) 將pre的右子節點置空。b) 打印cur的值。c) 將cur指針指向其右子節點。

class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> res;
        if (!root) return res;
        TreeNode *cur, *pre;
        cur = root;
        while (cur) {
            if (!cur->left) {
                res.push_back(cur->val);
                cur = cur->right;
            } else {
                pre = cur->left;
                while (pre->right && pre->right != cur) pre = pre->right;
                if (!pre->right) {
                    pre->right = cur;
                    cur = cur->left;
                } else {
                    pre->right = NULL;
                    res.push_back(cur->val);
                    cur = cur->right;
                }
            }
        }
        return res;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章