您的位置:首页 > 其它

逐层宽度优先搜索(BFS)二叉树各种解法

2016-01-14 08:02 337 查看


Binary Tree Level Order Traversal

Given a binary tree, return the level
order traversal of its nodes' values. (ie, from left to right, level by level).

Have you met this question in a real interview? 

Yes

Example

Given binary tree 
{3,9,20,#,#,15,7}
,
3
/ \
9  20
/  \
15   7


 

return its level order traversal as:
[
[3],
[9,20],
[15,7]
]


Challenge

Challenge 1: Using only 1 queue to implement it.
Challenge 2: Use DFS algorithm to do it.

Tags Expand  

BFS宽度优先搜索遍历二叉树实事求是是基本的不能再基本的基本的基本功了。
以前只会在纸上画一画。
今天终于能耐下心来自己跑一跑代码了。

原题地址:http://www.lintcode.com/en/problem/binary-tree-level-order-traversal/

直接bruteforce怎么奢侈怎么来

用两个数组a1 a2

第一个数字放树的根节点,然后遍历第一个数组,所有节点的左右子节点放到第二个数组。然后清空第一个数组。

遍历第二个数组a2,每个节点的左右子节点再放到第一个数组a1。然后清空第二个数组。

简单粗暴血淋淋。

Talk is cheap,show you the code

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

public class Solution {
/**
* @param root: The root of binary tree.
* @return: Level order a list of lists of integer
*/
public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
// write your code here

ArrayList<ArrayList<Integer>> result =new ArrayList<ArrayList<Integer>>();

if(root == null){
return result;
}
ArrayList<TreeNode> a1 = new ArrayList<TreeNode>();
ArrayList<TreeNode> a2 = new ArrayList<TreeNode>();

a1.add(root);

while( a1.size()!= 0 ){
a2.clear();
ArrayList<Integer> b = new ArrayList<Integer>();
for(int i = 0; i<a1.size(); i++){
b.add(a1.get(i).val);
}

for(int i = 0;i<a1.size();i++){
if(a1.get(i).left != null){
a2.add(a1.get(i).left);
}
if(a1.get(i).right != null){
a2.add(a1.get(i).right);
}
}

ArrayList<TreeNode> Tep = new ArrayList<TreeNode>();
Tep = a1;
a1 = a2;
a2 = Tep;

result.add(b);

}
return result;

}
}


后来听说有BFS可以通过DFS深度优先搜索的方式解决。觉得非常的惊讶。在以往的只是储备中觉得DFS和BFS是水火不相容的事情。通过一些资料才知道原来以前在内存只有X MB的那个年代,都是采取时间换空间的方式。应为BFS太奢侈了需要消耗很大空间,所以有大神通过控制DFS深度优先搜索的层数的方式来实现了BFS。索然耗费了时间但是却节约了宝贵的空间。

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

public class Solution {
/**
* @param root: The root of binary tree.
* @return: Level order a list of lists of integer
*/
public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
// write your code here

4000
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(root == null){
return result;
}
int maxDepth = 0;
while(true){
ArrayList<Integer> tepResult = new ArrayList<Integer>();
DFS(root , tepResult , 0 , maxDepth);
if(tepResult.size()==0){
break;
}
result.add(tepResult);
maxDepth++;
}
return result;

}

public void DFS(TreeNode root , ArrayList<Integer> result , int curDepth , int maxDepth){
if(root == null || curDepth > maxDepth){
return;
}

if(curDepth == maxDepth){
//add result
result.add(root.val);
return;
}
DFS(root.left , result , curDepth+1 , maxDepth);//left
DFS(root.right , result , curDepth+1 , maxDepth);//right
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: