Binary Tree Right Side View
给一个二叉树, 返回一个node组, 从右侧看二叉树的node. 就是简单的遍历, 记录一下树的高度, 因为从右侧看二叉树, 是这一层的最后一个. 所以遍历这个node的时候, 肯定是高度等于结果的大小.
public class Solution {
public List<Integer> rightSideView(TreeNode root) {
ArrayList<Integer> result = new ArrayList<Integer>();
if (root == null) return result;
return helper(root, result, 0);
}
public List<Integer> helper(TreeNode root, ArrayList<Integer> result,
int height) {
if (height == result.size()) {
result.add(root.val);
}
if (root.right != null) {
helper(root.right, result, height + 1);
}
if (root.left != null) {
helper(root.left, result, height + 1);
}
return result;
}
}