I made a tree and used DFS, too. Please have a look.

public class Solution {
protected class Node{
String word;
int numberOfTabs;
Node parent;
List<Node> children = new ArrayList<Node>();
public Node(String wordIn, int tabsIn){
word = wordIn;
numberOfTabs = tabsIn;
}
public void addChildren(Node child){
children.add(child);
}
public void setParent(Node papa){
parent = papa;
}
public Node getParent(){
return parent;
}
}
public int lengthLongestPath(String input) {
// if the file system does not have a file, return immediately
if(!input.contains(".")){
return 0;
}
Node root = null;
Node curr = null;
// start creating tree
int left = 0; int right = 0; int currTabs = 0; int prevTabs = 0;
while(right < input.length()){
while(right < input.length() && input.charAt(right)!='\n'){
right++;
}
String currWord = input.substring(left,right);
//System.out.println(currWord + " "+ currTabs+" "+ prevTabs);
right++;
left = right;
if(currTabs ==0){ // root
root = new Node(currWord, 0);
curr = root;
}
else{
Node node = new Node(currWord,currTabs);
if(currTabs > prevTabs){
}
else if(currTabs == prevTabs){
curr = curr.getParent();
}
else {
int diff = prevTabs - currTabs +1;
while(diff>0){
curr = curr.getParent();
diff--;
}
}
curr.addChildren(node);
node.setParent(curr);
curr = node;
}
// calculate tabs
while(right<input.length() && input.charAt(right)=='\t'){
right++;
}
prevTabs = currTabs;
currTabs = right - left;
left = right;
}
return giveLength(root);
}
static int giveLength(Node root){
System.out.println(root.word);
if(root.word.contains(".")){
//System.out.println(root.word);
return root.word.length()+root.numberOfTabs;
}
int max = 0;
for(int i=0; i<root.children.size();i++){
max = Math.max(max,giveLength(root.children.get(i)));
}
if(max ==0){
return 0;
}
return max+root.word.length();
}
}