算法:二叉树广度优先遍历 BFS
URL:https://leetcode.com/problems/binary-tree-right-side-view/
题目
1 | Binary Tree Right Side View |
分析
使用层序遍历(level traversal),即广度优先搜索 —— 借助Queue
难点是 标注每一层,来标注 每层的最后一个节点
当前层节点总数:count1,当前访问到节点数 i
下一层几点总数:count2
Java解法
1 | public class TreeNode { |
结果
Runtime: 1 ms, faster than 98.22% of Java online submissions for Binary Tree Right Side View.
Memory Usage: 36.3 MB, less than 100.00% of Java online submissions for Binary Tree Right Side View.
1次AC,Niubility
Python解法
1 |