二叉树BFS变形题目总结

二叉树广度优先遍历我们采用队列来实现,这里列举三道leetcode中有关二叉树BFS的题目,总的思想是一样的,题目要求的输出不同,我们需要根据不同的要求来编写,考察我们对于二叉树搜索的基本思想掌握以及java基础的掌握。

[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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值