广度优先遍历使用队列
注意队列的插入,使用链表实现的时候,插入要查到最后一个节点,而不是头节点的下一个节点。
其实栈用链表实现也很好。插入删除的效率都很好
队列用链表实现,插入效率低一下
![](https://img.haomeiwen.com/i6785781/ad6d88fd47069625.png)
102. 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).
这道题的主要难点在于如何记录树的深度,
应该是在构建每一层的时候,记录当层的宽度,而当层的由上一层的宽度决定,第一层宽度已知。遍历完一层,则应该深度+1。为了深度可以直接相加,注意储存节点前先判断是否为空
![](https://img.haomeiwen.com/i6785781/d41310be51c2f8ec.png)
199. Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
![](https://img.haomeiwen.com/i6785781/4f5da85367358078.png)
常出错的地方:
1 赋值和等号
2 变量名是否正确
3 返回值是否正确
4 使用未定义的变量和未使用定义了的变量+1
5 左括号写完
网友评论