广度优先搜索(Breadth-First-Search)
符合人类思维
深度优先搜索(Depth-First-Search)
计算机递归会采取栈的数据结构
推荐递归写法:
面试题
102. Binary Tree Level Order Traversal
?List是队列结构吗
BFS:
?补充Queue、Set的知识。
我的方法:
注意返回值,[] 的返回值与null不同;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list =new ArrayList<List<Integer>>();
if(root==null){return list;}
Queue<TreeNode> queue= new LinkedList<TreeNode>();
TreeNode node;
queue.add(root);
while(queue.peek()!=null){
int length=queue.size();
List<Integer> listElem = new ArrayList<Integer>();
for(int i=0;i<length;i++){
node=queue.remove();
listElem.add(node.val);
if(node.left!=null){
queue.add(node.left);
}
if(node.right!=null){
queue.add(node.right);
}
}
list.add(listElem);
}
return list;
}
}
DFS:
?
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
lot(root,1);
return res;
}
private void lot(TreeNode root, int level){
if(root==null) return;
if(res.size()<level){
List<Integer> temp = new ArrayList<>();
temp.add(root.val);
res.add(temp);
}
else{
List<Integer> temp = res.get(level-1);
temp.add(root.val);
res.set(level-1, temp);
}
lot(root.left, level+1);
lot(root.right, level+1);
}
}
104. Maximum Depth of Binary Tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){return 0;}
return maxD(root,1);
}
public int maxD(TreeNode node,int depth){
int left=depth;
int right=depth;
if(node == null){return depth;}
if(node.left != null){left= maxD(node.left,depth+1);}
if(node.right != null){right = maxD(node.right,depth+1);}
if(left>right){return left;}
return right;
}
}
111. Minimum Depth of Binary Tree
我的代码:
思路:BFS 逐层遍历,判断每个节点是否是叶子节点。如果是则跳出最外层循环。在每层全部遍历结束后,depth+1.
注意:while循环的非空判断,queue.isEmpty()当为空时,返回的是true。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null){return 0;}
Queue<TreeNode> queue= new LinkedList<TreeNode>();
queue.add(root);
int depth =1;
loop: while(!queue.isEmpty()){
int length = queue.size();
for(int i=0;i<length;i++){
TreeNode node = queue.poll();
if(node.left==null && node.right==null){
break loop;
}else{
if(node.left!=null){queue.add(node.left);}
if(node.right!=null){queue.add(node.right);}
}
}
depth+=1;
}
return depth;
}
}
DFS:递归,从子问题开始累加
class Solution {
public int minDepth(TreeNode root) {
//Base case
if(root == null) return 0;
//In case only one node is present
if(root.left == null && root.right == null) {
return 1;
}
// if no left subtree exists, calculate the minimum depth for the right subtree
if(root.left == null) {
return 1 + minDepth(root.right);
}
// if no right subtree exists, calculate the minimum depth for the left subtree
if(root.right == null) {
return 1 + minDepth(root.left);
}
//find minimum of the left and right height
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
}
作业:练习加注释