[LintCode 69. 242.] 二叉树的层次遍历

LintCode 69. 二叉树的层次遍历

问题描述

给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问)

样例

样例 1:
输入:{1,2,3}
输出:[[1],[2,3]]
解释:
   1
  / 
 2   3
它将被序列化为{1,2,3}

样例 2:
输入:{1,#,2,3}
输出:[[1],[2],[3]]
解释:
1
 
  2
 /
3
它将被序列化为{1,#,2,3}

挑战

挑战1:只使用一个队列去实现它

挑战2:用BFS算法来做

注意事项

首个数据为根节点,后面接着是其左儿子和右儿子节点值,"#"表示不存在该子节点。
节点数量不超过20。

解题思路

这道题相对以往依次输出元素的层次遍历有了一点变动,要求每一层的元素为一组来进行输出。
解决办法是,相对应的,队列中的结点也分组来入队出队操作。
更进一步,一次只操作一层结点并生成下一层的结点,那么取消队列,直接使用两个数组保存结点并轮换也是可以的。

参考代码

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: A Tree
     * @return: Level order a list of lists of integer
     */
    vector<vector<int>> levelOrder(TreeNode * root) {
        // write your code here
        if (root == NULL) return {};

        vector<TreeNode*> level;
        vector<TreeNode*> next;
        level.push_back(root);
        vector<vector<int>> res;
        vector<int> v;
        while (!level.empty()) {
            v.clear();
            next.clear();
            for (auto&& x : level) {
                v.push_back(x->val);
                if (x->left) {
                    next.push_back(x->left);
                }
                if (x->right) {
                    next.push_back(x->right);
                }
            }
            res.push_back(v);
            level.swap(next);
        }
        return res;
    }
};

LintCode 242. 将二叉树按照层级转化为链表

问题描述

给一棵二叉树,设计一个算法为每一层的节点建立一个链表。也就是说,如果一棵二叉树有 D 层,那么你需要创建 D 条链表。

样例

样例 1:

输入: {1,2,3,4}
输出: [1->null,2->3->null,4->null]
解释: 
        1
       / 
      2   3
     /
    4
样例 2:

输入: {1,#,2,3}
输出: [1->null,2->null,3->null]
解释: 
    1
     
      2
     /
    3

解题思路

与上面类似,每一层作为一组,使用两个数组轮换来获得每一层的结点。

参考代码

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    /**
     * @param root the root of binary tree
     * @return a lists of linked list
     */
    vector<ListNode*> binaryTreeToLists(TreeNode* root) {
        // Write your code here
        if (root == NULL) return {};
        vector<ListNode*> res;
        vector<TreeNode*> level;
        vector<TreeNode*> next;
        ListNode dummy;

        level.push_back(root);
        while (!level.empty()) {
            next.clear();
            dummy.next = NULL;
            ListNode* p = &dummy;
            for (auto&& x : level) {
                p->next = new ListNode(x->val);
                p = p->next;
                if (x->left) next.push_back(x->left);
                if (x->right) next.push_back(x->right);
            }
            res.push_back(dummy.next);
            level.swap(next);
        }
        return res;
    }
};
原文地址:https://www.cnblogs.com/zhcpku/p/14261110.html