Binary Tree Level Order Traversal

/**
 * 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;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值