Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public List<Integer> rightSideView(TreeNode root) {
  12. List<Integer> res = new LinkedList<>();
  13. if (root == null) return res;
  14. Queue<TreeNode> q = new LinkedList<>();
  15. q.offer(root);
  16. while (!q.isEmpty()) {
  17. TreeNode last = null;
  18. int cnt = q.size();
  19. for (int i = 0; i < cnt; ++i) {
  20. last = q.poll();
  21. if (last.left != null) q.offer(last.left);
  22. if (last.right != null) q.offer(last.right);
  23. }
  24. res.add(last.val);
  25. }
  26. return res;
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement