Java Solution, Accepted

  • 2

    The idea is to add the rightest element to the list, all of other elements need to be skipped.

    1. Create currentLevel parameter to judge which level you are

    2. Judge current list size whether contains current level elements

      public void levelOrder(TreeNode root, int currentLevel,
      List<Integer> rideSideNodes) {
      if (root == null)
      if (currentLevel > rideSideNodes.size()) {
      levelOrder(root.right, currentLevel + 1, rideSideNodes);
      levelOrder(root.left, currentLevel + 1, rideSideNodes);

      public List<Integer> rightSideView(TreeNode root) {
      List<Integer> rideSideNodes = new ArrayList<Integer>();
      int currentLevel = 1;
      levelOrder(root, currentLevel, rideSideNodes);
      return rideSideNodes;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.