二叉树广度优先遍历我们采用队列来实现,这里列举三道leetcode中有关二叉树BFS的题目,总的思想是一样的,题目要求的输出不同,我们需要根据不同的要求来编写,考察我们对于二叉树搜索的基本思想掌握以及java基础的掌握。
[b]1,Binary Tree Level Order Traversal[/b]
给定一颗二叉树,用广度优先搜索遍历所有节点,并输出结果。
例如:
3
/ \
9 20
/ \
15 7
输出:
[
[3],
[9,20],
[15,7]
]
这是基本的二叉树水平遍历,我们只需要借助几个变量,来辅助记录什么时候应该记录结果,什么时候遍历下一层就可以了,代码如下:
[b]2,Binary Tree Level Order Traversal II[/b]
给定一颗二叉树,用广度优先搜索遍历所有节点,并输出结果。
例如:
3
/ \
9 20
/ \
15 7
输出:
[
[15,7],
[9,20],
[3]
]
第二道题与第一道题的输出结果不一样,是从最后一层开始的,这就考察我们对于Linkedlist的了解程度,我们用它的addFirst()方法可以轻松的将结果逆序,代码如下:
[b]3,Binary Tree Zigzag Level Order Traversal[/b]
给定一颗二叉树,用广度优先搜索遍历所有节点,并输出结果。
例如:
3
/ \
9 20
/ \
15 7
输出:
[
[3],
[20,9],
[15,7]
]
这道题要求我们以“Z”字形来输出结果,我们可以用一个标记来记录当然的层数,如果是奇数层我们就正序添加节点到结果集中,如果是偶数层我们就逆序添加节点到结果集中,同样采用addFirst()方法,代码如下:
[b]1,Binary Tree Level Order Traversal[/b]
给定一颗二叉树,用广度优先搜索遍历所有节点,并输出结果。
例如:
3
/ \
9 20
/ \
15 7
输出:
[
[3],
[9,20],
[15,7]
]
这是基本的二叉树水平遍历,我们只需要借助几个变量,来辅助记录什么时候应该记录结果,什么时候遍历下一层就可以了,代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> llist = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
int count = 0;
int helper = 1;
if(root == null) return llist;
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if(helper > 0){
list.add(node.val);
helper --;
}
if(node.left != null) {
queue.offer(node.left);
count ++;
}
if(node.right != null) {
queue.offer(node.right);
count ++;
}
if(helper == 0) {
llist.add(new ArrayList<Integer>(list));
list.clear();
helper = count;
count = 0;
}
}
return llist;
}
}
[b]2,Binary Tree Level Order Traversal II[/b]
给定一颗二叉树,用广度优先搜索遍历所有节点,并输出结果。
例如:
3
/ \
9 20
/ \
15 7
输出:
[
[15,7],
[9,20],
[3]
]
第二道题与第一道题的输出结果不一样,是从最后一层开始的,这就考察我们对于Linkedlist的了解程度,我们用它的addFirst()方法可以轻松的将结果逆序,代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
LinkedList<List<Integer>> llist = new LinkedList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
if(root == null) return llist;
queue.offer(root);
int count = 0;
int helper = 1;
while(!queue.isEmpty()) {
TreeNode node = queue.poll();
if(helper > 0) {
list.add(node.val);
helper --;
}
if(node.left != null) {
queue.offer(node.left);
count ++;
}
if(node.right != null) {
queue.offer(node.right);
count ++;
}
if(helper == 0) {
llist.addFirst(new ArrayList<Integer>(list));
list.clear();
helper = count;
count = 0;
}
}
return llist;
}
}
[b]3,Binary Tree Zigzag Level Order Traversal[/b]
给定一颗二叉树,用广度优先搜索遍历所有节点,并输出结果。
例如:
3
/ \
9 20
/ \
15 7
输出:
[
[3],
[20,9],
[15,7]
]
这道题要求我们以“Z”字形来输出结果,我们可以用一个标记来记录当然的层数,如果是奇数层我们就正序添加节点到结果集中,如果是偶数层我们就逆序添加节点到结果集中,同样采用addFirst()方法,代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
LinkedList<List<Integer>> llist = new LinkedList<List<Integer>>();
LinkedList<Integer> list = new LinkedList<Integer>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
int flag = 1;
int count = 0;
int helper = 1;
if(root == null) return llist;
queue.offer(root);
while(!queue.isEmpty()) {
TreeNode tem = queue.poll();
if(helper > 0) {
if(flag % 2 == 1) {
list.add(tem.val);
} else {
list.addFirst(tem.val);
}
helper --;
}
if(tem.left != null) {
queue.offer(tem.left);
count ++;
}
if(tem.right != null) {
queue.offer(tem.right);
count ++;
}
if(helper == 0) {
llist.add(new LinkedList<Integer>(list));
helper = count;
count = 0;
flag ++;
list.clear();
}
}
return llist;
}
}