94. Binary Tree Inorder Traversal

基础训练:

递归:
1.

/**
 * 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 {
private:
    vector<int> res;
public:
    vector<int> inorderTraversal(TreeNode* root) {
        if(root==NULL) return {};
        if(root->left) inorderTraversal(root->left);
        res.push_back(root->val);
        if(root->right) inorderTraversal(root->right);
        return res;
    }
};
/**
 * 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 {
private:
    void dfs(TreeNode* root,vector<int>& res){
        if(root==nullptr) return;
        if(root->left) dfs(root->left,res);
        res.push_back(root->val);
        if(root->right) dfs(root->right,res);
        return;
    }
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        dfs(root,res);
        return res;
    }
};

迭代:
1.

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

Morris 算法,可以节省栈的空间。主要思路是重构树的结构,按照前序、中序或后续的顺序连接成链表的形式。
这里按照中序的顺序。那么就将root左子树最右边的节点的right指向root,相当于root和root->right即右子树,都挂在root此时左子树的最右的节点的right指针上,然后root=root->left,所以还需要一个变量将root->left指为NULL,然后重复上述步骤,最后得到一个链表,每个节点只有right指向一个节点,root将会指向最开始左子树的最左侧的节点,然后循环会将root一个一个加入进res数组。
详细解释

/**
 * 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> res;
        TreeNode* pre=nullptr;
        while(root!=nullptr){
            if(root->left){
                pre = root->left;
                while(pre->right!=nullptr){
                    pre=pre->right;
                }
                pre->right=root;
                TreeNode* dele=root;
                root=root->left;
                dele->left=NULL;
            }
            else{
                res.push_back(root->val);
                root = root->right;
            }
        }
        return res;
    }
};
posted @ 2021-04-22 21:23  Mrsdwang  阅读(35)  评论(0编辑  收藏  举报