/* * 199. Binary Tree Right Side View * 11.21 By Mingyang * 在recursive的算法,就是贴着树的右边界往下面走,如果不行往左边偏一个,然后继续往右边偏,利用末尾的个数与层数相等的技巧 * 其实是一种根右左的算法,很巧妙 */ public List<Integer> ss = new ArrayList<Integer>(); public List<Integer> rightSideView(TreeNode root) { if (root == null) return ss; rightHelper(root, ss, 0); return ss; } public void rightHelper(TreeNode root, List<Integer> res, int k) { if (root == null) return; if (k == res.size()) res.add(root.val); rightHelper(root.right, res, k + 1); rightHelper(root.left, res, k + 1); }