leetcode二叉树遍历

来源:互联网 发布:电子图书数据库有哪些 编辑:程序博客网 时间:2024/06/10 01:05

二叉树的遍历主要以根节点为中心展开思路去写,这里先讲下前序遍历非递归,通常的数组二叉树,

都是以标记进行,这里由于是链式二叉树,所以可以通过把每个节点的左右节点指向变为NULL,这样

第二次遍历到就可以通过左右节点进行判断,从而加入结果遍历中:

后序遍历非递归:

class Solution {public:    vector<int> postorderTraversal(TreeNode *root) {        vector<int> v;        if(root==NULL)            return v;        stack<TreeNode*> s;        s.push(root);        while(!s.empty()){            TreeNode *cur=s.top();            if(cur->left==NULL&&cur->right==NULL){                v.push_back(cur->val);                s.pop();            }             else{                if(cur->right){                    s.push(cur->right);                    cur->right=NULL;                }                if(cur->left){                    s.push(cur->left);                    cur->left=NULL;                }            }        }        return v;    }};

后序遍历递归:

非递归主要注意vector传值的时候也要用引用。

class Solution {public:    vector<int> postorderTraversal(TreeNode *root) {        vector<int> v;        if(root==NULL)            return v;        postOrder(root,v);        return v;    }    void postOrder(TreeNode *root,vector<int> &v){        if(root->left)            postOrder(root->left,v);        if(root->right)            postOrder(root->right,v);        v.push_back(root->val);    }};
前序遍历非递归:

class Solution {public:    vector<int> preorderTraversal(TreeNode *root) {        vector<int> v;        if(root==NULL)            return v;        stack<TreeNode*> s;        s.push(root);        while(!s.empty()){           TreeNode *cur=s.top();            s.pop();            v.push_back(cur->val);            if(cur->right){                s.push(cur->right);            }            if(cur->left){                s.push(cur->left);            }        }        return v;    }};
前序遍历递归:

class Solution {public:    vector<int> preorderTraversal(TreeNode *root) {        vector<int> v;        if(root==NULL)            return v;        preOrder(root,v);        return v;    }    void preOrder(TreeNode *root,vector<int> &v){        v.push_back(root->val);        if(root->left)            preOrder(root->left,v);        if(root->right)            preOrder(root->right,v);    }};
中序遍历递归:

vector<int> inorderTraversal(TreeNode *root) {            vector<int> v;            if(root==NULL)                return v;            inOrder(root,v);            return v;        }        void inOrder(TreeNode *root,vector<int> &v){            if(root->left)                inOrder(root->left,v);            v.push_back(root->val);            if(root->right)                inOrder(root->right,v);        }
中序遍历非递归:

和后序非递归类似,记得指针赋值NULL。

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






0 0
原创粉丝点击