Java - TreeSet


  • 0
    A
    public int findSecondMinimumValue(TreeNode root) {
          TreeSet<Integer> ts = new TreeSet<Integer>();
          traverse(root, ts);
          ts.pollFirst();
          if(!ts.isEmpty()) return ts.first(); else return -1;
      }
      public void traverse(TreeNode node, TreeSet<Integer> ts){
          if(node!=null){
              ts.add(node.val);
              traverse(node.left,ts);
              traverse(node.right,ts); 
          }
      }

Log in to reply
 

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