逐層寬度優先搜索(BFS)二叉樹各種解法


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
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
}


}




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com