string representation for binary trees
/**
* Consider this string representation for binary trees. Each node is of the form (lr), where l represents the left child and r represents the right child. If l is the character 0, then there is no left child. Similarly, if r is the character 0, then there is no right child. Otherwise, the child can be a node of the form (lr), and the representation continues recursively.
For example: (00) is a tree that consists of one node. ((00)0) is a two-node tree in which the root has a left child, and the left child is a leaf. And ((00)(00)) is a three-node tree, with a root, a left and a right child.
Write a function that takes as input such a string, and returns -1 if the string is malformed, and the depth of the tree if the string is well-formed.
For instance:
find_depth('(00)') -> 0
find_depth('((00)0)') -> 1
find_depth('((00)(00))') -> 1
find_depth('((00)(0(00)))') -> 2
find_depth('((00)(0(0(00))))') -> 3
find_depth('x') -> -1
find_depth('0') -> -1
find_depth('()') -> -1
find_depth('(0)') -> -1
find_depth('(00)x') -> -1
find_depth('(0p)') -> -1
* @author pjayachandran
*
*/待解决:
public class StringBinaryTreeDepth {
public static int getTreeDepth(String inp) {
if(inp == null) {
return -1;
}
if(inp.length() < 4) {
return -1;
}
if(inp.charAt(0) != '(' || inp.charAt(inp.length() -1) != ')') {
return -1;
}
int depth = -1;
int zeroCount = 0;
Stack<Character> stack = new Stack<Character>();
for(int i=0; i<inp.length(); i++) {
char c = inp.charAt(i);
if(c == '(') {
stack.push(c);
if(stack.size() > depth) {
depth = stack.size();
}
zeroCount = 0;
} else if(c == ')') {
stack.pop();
zeroCount = 0;
} else {
if( c != '0') {
return -1;
} else {
zeroCount += 1;
if(zeroCount > 2) {
return -1;
}
}
}
}
// invalid pattern
if(stack.size() > 0) {
return -1;
}
return depth-1;
}
} 相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
ChaITSimpleLove 2020-10-06
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25