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.
For example:
Given the following binary tree,
1 <--- / \ 2 3 <--- \ \ 5 4 <---
You should return [1, 3, 4]
.
题目含义:从右面观察一颗二叉树,输出由上到下打印的节点
1 public List<Integer> rightSideView(TreeNode root) { 2 List<Integer> result = new ArrayList<>(); 3 if (root == null) return result; 4 Queue<TreeNode> q = new LinkedList<>(); 5 q.add(root); 6 while (!q.isEmpty()) 7 { 8 int size = q.size(); 9 TreeNode node = null; 10 for (int i=0;i<size;i++) 11 { 12 node = q.poll(); 13 if (node.left !=null) q.offer(node.left); 14 if (node.right !=null) q.offer(node.right); 15 } 16 result.add(node.val); 17 } 18 return result; 19 }