共计 5427 个字符,预计需要花费 14 分钟才能阅读完成。
题目地址
我的解法
/**
* 2ms 21.07
* 41M 33.17
* 其实是广度优先搜寻
* 切入点是一层一层看,依据以后层构建下一层。* */
public static int maxDepth(TreeNode root) {if (null == root) {return 0;}
int deep = 1;
List<TreeNode> curLevel = new ArrayList<>();
curLevel.add(root);
List<TreeNode> nextLevel = null;
while (hasNextLevel(curLevel)) {nextLevel = new ArrayList<>();
for (TreeNode cur : curLevel) {if (null != cur) {nextLevel.add(cur.left);
nextLevel.add(cur.right);
}
}
curLevel = nextLevel;
deep ++;
}
return deep;
}
官网 - 广度优先搜寻
/**
* 官网的广度优先搜寻
* 使用了 Queue @see MyQueue
* */
public static int maxDepth_l1(TreeNode root) {if (null == root) {return 0;}
Queue<TreeNode> curLevel = new LinkedList<>();
curLevel.offer(root);
int deep = 0;
while (!curLevel.isEmpty()) {int size = curLevel.size();//TODO 固定 poll 次数
while (size > 0) {TreeNode curNode = curLevel.poll();
if (curNode == null) {continue;}
if (curNode.left != null) {curLevel.offer(curNode.left);
}
if (curNode.right != null) {curLevel.offer(curNode.right);
}
size --;
}
// Queue<TreeNode> curLevelTemp = curLevel;
// for (TreeNode cur : curLevelTemp) {java.util.ConcurrentModificationException
// if (cur == null) {
// continue;
// }
// if (cur.left != null) {// curLevel.offer(cur.left);
// }
// if (cur.right != null) {// curLevel.offer(cur.right);
// }
// curLevel.poll();
// }
deep ++;
}
return deep;
}
官网 - 深度优先搜寻
/**
* 官网深度优先搜寻
* 使用递归
* 如果咱们晓得了左子树和右子树的最大深度 l 和 r,那么该二叉树的最大深度即为
*
* max(l,r) + 1
*
* 而左子树和右子树的最大深度又能够以同样的形式进行计算。* */
public static int maxDepth_l2(TreeNode root) {
// 3
// / \
// 9 20
// / \
// 15 7
if (root == null) {return 0;} else {int maxLeft = maxDepth_l2(root.left);//1
int maxRight = maxDepth_l2(root.right);
return Math.max(maxLeft, maxRight) + 1;
}
}
拓展 -DFS&BFS
深度优先搜索算法 DFS
1、算法步骤:
——拜访顶点 v;
——顺次从 v 的未被拜访的邻接点登程,对图进行深度优先遍历;直至图中和 v 有门路相通的顶点都被拜访;
——若此时图中尚有顶点未被拜访,则从一个未被拜访的顶点登程,从新进行深度优先遍历,直到图中所有顶点均被拜访过为止
2、DFS 属于自觉搜寻。深度优先搜寻是图论中的经典算法,利用深度优先搜索算法能够产生指标图的相应拓扑排序表,利用拓扑排序表能够不便的解决很多相干的图论问题,如最大门路问题等等。个别用堆数据结构来辅助实现 DFS 算法。
广度优先搜索算法 BFS
1、算法步骤:
——首先将根节点放入队列中;
——从队列中取出第一个节点,并测验它是否为指标。如果找到指标,则完结搜查并回传后果。否则将它所有尚未测验过的间接子节点退出队列中;
——若队列为空,示意整张图都查看过了——亦即图中没有欲搜查的指标。完结搜查并回传“找不到指标”;
——反复步骤 2
2、BFS 同样属于自觉搜寻。简略的说,BFS 是从根节点开始,沿着树 (图) 的宽度遍历树 (图) 的节点。如果所有节点均被拜访,则算法停止。个别用队列数据结构来辅助实现 BFS 算法。
拓展 API-Queue
个性
1、线性表 / 线性队列
2、FIFO
3、表头出,表尾加
implements
Queue<String> queue_ArrayBlockingQueue = new ArrayBlockingQueue<>(10);
Queue<String> queue_ArrayDeque = new ArrayDeque<>();
Queue<String> queue_ConcurrentLinkedDeque = new ConcurrentLinkedDeque<>();
Queue<String> queue_LinkedBlockingDeque = new LinkedBlockingDeque<>();
Queue<String> queue_LinkedList = new LinkedList<>();
methods
method | 个性 |
---|---|
add | 队列尾插入,队列满时抛出 IllegalStateException |
offer | 队列尾插入,队列满时返回 false |
remove | 移除并返回头部元素,队列空时,抛出 NoSuchElementException |
pool | 移除并返回头部元素,队列空时,返回 null |
element | 查看队列头元素,队列空时,抛出 NoSuchElementException |
peek | 查看队列头元素,队列空时,返回 null |
code
public static void main(String[] args) {Queue<String> queue_ArrayBlockingQueue = new ArrayBlockingQueue<>(10);
Queue<String> queue_ArrayDeque = new ArrayDeque<>();
Queue<String> queue_ConcurrentLinkedDeque = new ConcurrentLinkedDeque<>();
Queue<String> queue_LinkedBlockingDeque = new LinkedBlockingDeque<>();
Queue<String> queue_LinkedList = new LinkedList<>();
System.out.println("++++++++++++++++++++++++++");
//add 表尾插 ——>[1,2,3]
// 队列满时,抛出 IllegalStateException
Queue<Integer> queue_add = new ArrayBlockingQueue<>(2);
queue_add.add(1);
queue_add.add(2);
try {queue_add.add(3);
} catch (Exception e) {System.out.println(e);
}
for (Integer q : queue_add) {System.out.println(q);
}
try {Queue<Integer> queue_LinkedList_add = new LinkedList<>();
for (int i = 0; i < 1000000; i++) {queue_LinkedList_add.add(i);
}
System.out.println(queue_LinkedList_add.size());
} catch (Exception e) {System.out.println(e);
}
System.out.println("++++++++++++++++++++++++++");
//offer 表尾插 ——>[1,2,3,4,5,6]
// 队列满时,返回 false
Queue<Integer> queue_offer = new ArrayBlockingQueue<>(4);
System.out.println(queue_offer.offer(1));
System.out.println(queue_offer.offer(2));
System.out.println(queue_offer.offer(3));
System.out.println(queue_offer.offer(4));
try {System.out.println(queue_offer.offer(5));
} catch (Exception e) {System.out.println(e);
}
for (Integer q : queue_offer) {System.out.println(q);
}
try {Queue<Integer> queue_LinkedList_offer = new LinkedList<>();
for (int i = 0; i < 1000000; i++) {if (!queue_LinkedList_offer.add(i)) {System.out.println("offer false");
}
}
System.out.println(queue_LinkedList_offer.size());
} catch (Exception e) {System.out.println(e);
}
System.out.println("++++++++++++++++++++++++++");
//remove 移除并返回队列头部元素
// 队列空时,抛出 NoSuchElementException
Queue<Integer> queue_LinkedList_remove = new LinkedList<>();
for (int i = 0; i < 6; i++) {queue_LinkedList_remove.offer(i);
}
for (int i = 0; i < 7; i++) {
try {System.out.println(queue_LinkedList_remove.remove());
} catch (Exception e) {System.out.println(i + "e =" + e);
}
}
System.out.println("++++++++++++++++++++++++++");
//poll 移除并返回队列头部元素
// 队列空时,返回 null
Queue<Integer> queue_poll = new LinkedList<>();
for (int i = 0; i < 10; i++) {queue_poll.offer(i);
}
for (int i = 0; i < 11; i++) {
try {System.out.println(queue_poll.poll());
} catch (Exception e) {System.out.println(i + "e =" + e);
}
}
System.out.println("++++++++++++++++++++++++++");
//element 返回队列头部元素
// 队列空时,抛出 NoSuchElementException
Queue<Integer> queue_element = new LinkedList<>();
queue_element.offer(1);
queue_element.offer(2);
System.out.println(queue_element.element());
System.out.println(queue_element.poll());
System.out.println(queue_element.element());
System.out.println(queue_element.poll());
try {System.out.println(queue_element.element());
} catch (Exception e) {System.out.println(e);
}
System.out.println("++++++++++++++++++++++++++");
//peek 返回队列头部元素
// 队列空时,返回 null
Queue<Integer> queue_peek = new LinkedList<>();
for (int i = 0; i < 3; i++) {queue_peek.offer(i);
}
for (int i = 0; i < 4; i++) {
try {System.out.println(queue_peek.peek());
System.out.println(queue_peek.poll());
} catch (Exception e) {System.out.println(i + "e =" + e);
}
}
System.out.println("++++++++++++++++++++++++++");
}
正文完