Binary Tree Level Order Traversal

来源:互联网 发布:淘宝没有3c认证投诉 编辑:程序博客网 时间:2024/05/05 21:56
/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    vector<vector<int> > levelOrder(TreeNode *root) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<vector<int> > res;        if (!root) return res;        queue<TreeNode *> q;        q.push(root);        vector<int> first;        first.push_back(root->val);        res.push_back(first);        int num=1;        while (q.size()>0) {            int count=0;            vector<int> nodes;            for (int i=0; i<num; i++) {                TreeNode *temp=q.front();                q.pop();                if (temp->left) {                    nodes.push_back(temp->left->val);                    q.push(temp->left);                    count++;                }                if (temp->right) {                    nodes.push_back(temp->right->val);                    q.push(temp->right);                    count++;                }            }            num=count;            if (nodes.size()!=0) {                res.push_back(nodes);            }        }        return res;    }};


The Second time I did this question (also BFS):

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    vector<vector<int> > levelOrder(TreeNode *root) {        // Note: The Solution object is instantiated only once and is reused by each test case.        vector<vector<int>> res;        if (!root) return res;        queue<TreeNode*> q;        q.push(root);        int n1=1, n2=0;        while(q.size()>0) {            vector<int> cur;            while (n1>0) {                TreeNode* temp=q.front();                cur.push_back(temp->val);                q.pop();                n1--;                if (temp->left) {                    q.push(temp->left);                    n2++;                }                if (temp->right) {                    q.push(temp->right);                    n2++;                }            }            res.push_back(cur);            n1=n2;            n2=0;        }        return res;    }};


II (if use DFS)

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    void levelOrder(TreeNode *root, int level, vector<vector<int> > &res) {        if (!root) return;        if (level>res.size()) {            vector<int> nodes;            res.push_back(nodes);        }        res[level-1].push_back(root->val);        levelOrder(root->left, level+1, res);        levelOrder(root->right, level+1, res);    }    vector<vector<int> > levelOrderBottom(TreeNode *root) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<vector<int> > res;        levelOrder(root,1,res);        std::reverse(res.begin(), res.end());        return res;    }};

Zigzag

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    vector<vector<int> > zigzagLevelOrder(TreeNode *root) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<vector<int> > res;        if (!root) return res;        queue<TreeNode *> q;        q.push(root);        vector<int> first;        first.push_back(root->val);        res.push_back(first);        int num=1;        int forward=0;        while (q.size()>0) {            int count=0;            vector<int> nodes;            for (int i=0; i<num; i++) {                TreeNode *temp=q.front();                q.pop();                if (temp->left) {                    nodes.push_back(temp->left->val);                    q.push(temp->left);                    count++;                }                if (temp->right) {                    nodes.push_back(temp->right->val);                    q.push(temp->right);                    count++;                }            }            num=count;            if (nodes.size()!=0) {                if (forward==0) {                    std::reverse(nodes.begin(), nodes.end());                }                res.push_back(nodes);            }            if (forward==0) {                forward=1;            }else if (forward==1) {                forward=0;            }        }        return res;    }};


原创粉丝点击