leetcode102 二叉树的层序遍历

从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回:
[3,9,20,15,7]

提示:
节点总数 <= 1000

代码实现:

#define INT_MAX 1010
int* levelOrder(struct TreeNode* root, int* returnSize) {
     
	if (root == NULL) {
     
		* returnSize = 0;
		return root;
	}
	struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode) * INT_MAX);
	int* res = (int*)malloc(sizeof(int) * INT_MAX);
	int head = 0;
	int tail = 0;
	int len = 0;
	queue[tail++] = root;
	while (head < tail) {
     
		int size = tail - head;
		for (int i = 0; i < size; i++) {
     
			struct TreeNode* p = queue[head++];
			res[len++] = p -> val;
			if (p -> left) {
     
				queue[tail++] = p -> left;
			}
			if (p -> right) {
     
				queue[tail++] = p -> right;
			}
		}
	}
	* returnSize = len;
	return res;
}

第二题、为上题的变种

从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

提示:
节点总数 <= 1000

代码实现:

#define INT_MAX 1010
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {
     
	if (root == NULL) {
     
		* returnSize = 0;
		return root;
	}
	struct TreeNode** queue[INT_MAX];
	int** res = (int**)malloc(sizeof(int*) * INT_MAX);
	* returnColumnSizes = (int*)malloc(sizeof(int) * INT_MAX);
	int head = 0;
	int tail = 0;
	int level = 0;
	queue[tail++] = root;
	while (head < tail) {
     
		int size = tail - head;
		res[level] = (int*)malloc(sizeof(int) * size);
		(* returnColumnSizes)[level] = size;
		for (int i = 0; i < size; i++) {
     
			struct TreeNode* p = queue[head++];
			if (p -> left) {
     
				queue[tail++] = p -> left;
			}
			if (p -> right) {
     
				queue[tail++] = p -> right;
			}
			res[level][i] = p -> val;
		}
		level++;
	}
	(* returnSize) = level;
	return res;
}

从上到下打印二叉树3
请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回其层次遍历结果:

[
  [3],
  [20,9],
  [15,7]
]

提示:
节点总数 <= 1000

class Solution {
     
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
     
        vector <vector<int>> res;
        if (root == NULL) {
     
            return res;
        }
        queue <TreeNode*> q;
        q.push(root);
        int level = 0;
        while (q.size()) {
     
            level++;
            vector <int> cols;
            int size = q.size();
            for (int i = 0; i < size; i++) {
     
                TreeNode* p = q.front();
                cols.push_back(p -> val);
                if (p -> left) {
     
                    q.push(p -> left);
                }
                if (p -> right) {
     
                    q.push(p -> right);
                }
                q.pop();
            }
            if (level % 2 == 0) {
     
                reverse(cols.begin(), cols.end());
            }
            if (cols.size()) {
     
                res.push_back(cols);
            }
        }
        return res;
    }
};

你可能感兴趣的:(图论,bfs,queue,队列,二叉树)