做题思路:层序遍历
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ //层次遍历 class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> list=new ArrayList<>(); if(root==null){ return list; } Queue<TreeNode> queue=new LinkedList<>(); queue.offer(root); while(!queue.isEmpty()){ int count=queue.size(); for(int i=0;i<count;i++){ TreeNode node=queue.poll(); if(node.left!=null){ queue.offer(node.left); } if(node.right!=null){ queue.offer(node.right); } if(i==count-1){ list.add(node.val); } } } return list; } }