public static List<Integer> rightSideView(TreeNode root) {
ArrayList<TreeNode> list = new ArrayList<TreeNode>();
ArrayList<Integer> resultList = new ArrayList<Integer>();
if (root == null) {
return resultList;
}
list.add(root);
addRightSideNode(list, resultList);
return resultList;
}
public static void addRightSideNode(ArrayList<TreeNode> list,ArrayList<Integer> resultList) {
if (list !=null &&list.size() == 0) {
return;
}
resultList.add(list.get(list.size() - 1).val);
ArrayList<TreeNode> newList = new ArrayList<TreeNode>();
for (TreeNode node : list) {
if (node.left != null) {
newList.add(node.left);
}
if (node.right != null) {
newList.add(node.right);
}
}
addRightSideNode(newList, resultList);
}
网友评论