MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / recur

Method recur

RecoveraTreeFromPreorderTraversal.java:25–46  ·  view source on GitHub ↗
(String traversal, int depth)

Source from the content-addressed store, hash-verified

23 return recur(traversal,0);
24 }
25 public TreeNode recur(String traversal, int depth){
26 if(index >= n) return null;
27 //count the dash
28 int count=0;
29 int tempI=index;
30 while(tempI < n && !Character.isDigit(traversal.charAt(tempI))){
31 count++;
32 tempI++;
33 }
34 if(count!=depth) return null;
35 index = tempI;
36 //find the number
37 int val=0;
38 while(index < n && Character.isDigit(traversal.charAt(index))){
39 val = val * 10 + (traversal.charAt(index) - '0');
40 index++;
41 }
42 TreeNode node = new TreeNode(val);
43 node.left = recur(traversal, depth+1);
44 node.right = recur(traversal, depth+1);
45 return node;
46 }
47
48}
49

Callers 1

recoverFromPreorderMethod · 0.95

Calls 3

popMethod · 0.80
pushMethod · 0.80
isEmptyMethod · 0.45

Tested by

no test coverage detected