代码分析
实际上就是二叉树的遍历的变形,将高度考虑进入即可
代码实现
import java.util.ArrayList; import java.util.List; /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public List<Integer> rightSideView(TreeNode root) { ArrayList<Integer> res=new ArrayList(); f(root,res,0); return res; } public void f(TreeNode root,ArrayList<Integer> res,int lindex) { if(root==null) return ; if(res.size()<=lindex) { res.add(root.val); }else { res.set(lindex,root.val); } f(root.left,res,lindex+1); f(root.right,res,lindex+1); } }