逐层宽度优先搜索(BFS)二叉树各种解法
2016-01-14 08:02
337 查看
Binary Tree Level Order Traversal
Given a binary tree, return the levelorder 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 } }
相关文章推荐
- Python list 排序
- 二十九(Sparse coding练习)
- 二十八(使用BP算法思想求解Sparse coding中矩阵范数导数)
- 1602: [Usaco2008 Oct]牧场行走|Lca
- 二十七(Sparse coding中关于矩阵的范数求导)
- STL vector提高效率注意事项与技巧
- 二十六(Sparse coding简单理解)
- 二十五(Kmeans单层网络识别性能)
- 二十四(stacked autoencoder练习)
- 二十三(Convolution和Pooling练习)
- 二十二(linear decoder练习)
- 二十一(随机初始化在无监督特征学习中的作用)
- Spark Shuffle之Tungsten Sort Shuffle
- 【BLE】CC2541之通过广播发送自定义数据
- iOS开发-UITableView顶部图片下拉放大
- [建议]我对软工有话说(下)
- 8.2.6 PEB —— PEB结构值不正确的问题
- 欢迎使用CSDN-markdown编辑器
- Python字典
- 树莓派个人实测 Q&A(最新修改使用Manjaro连接远程桌面) (二)