我计划建立一个商业用途的网站.我最近听说过Play框架,看起来不错.但我不确定我是否应该使用它.我知道java,jsp,servlets和struts 1.
我不知道该框架有多稳定?我应该使用本机Java EE应用程序还是应该使用Play?
以下是我找到第一个共同祖先的算法.但我不知道如何计算它的时间复杂度,任何人都可以帮忙吗?
public Tree commonAncestor(Tree root, Tree p, Tree q) {
if (covers(root.left, p) && covers(root.left, q))
return commonAncestor(root.left, p, q);
if (covers(root.right, p) && covers(root.right, q))
return commonAncestor(root.right, p, q);
return root;
}
private boolean covers(Tree root, Tree p) { /* is p a child of root? */
if (root == null) return false;
if (root == p) return true;
return covers(root.left, p) || covers(root.right, p);
}
Run Code Online (Sandbox Code Playgroud) 我想要计算叶子节点的数量:注意:不能使用全局/类级别变量我跟随算法,它工作正常.但我希望方法签名是
countLeaves(Node node)
Run Code Online (Sandbox Code Playgroud)
我知道我可以重载methds并从1个args调用2 args方法sig,但是不想这样做.任何人都可以建议任何其他方法吗?
int countLeaves(Node node,int count){
if(node==null)
return 0;
if(node.left==null && node.right==null){
return 1+count;
}else{
int lc = countLeaves(node.left, count);
int total = countLeaves(node.right, lc);
return total;
}
}
Run Code Online (Sandbox Code Playgroud) 找到二叉树的宽度.
在我的每个假期的代码中,我在哈希映射中创建一个条目,并在我离开i时找到一个节点时不断更新它.最后我将迭代hashmap以找到最大宽度.但是我怎么能不使用任何classleel/global varaiables?
Map<Integer,Integer> mp = new HashMap<Integer,Integer>();
void width(Node node,int level){
if(node==null)
return;
if(mp.containsKey(level)){
int count = mp.get(level);
mp.put(level, count+1);
}else{
mp.put(level, 1);
}
width(node.left, level+1);
width(node.right, level+1);
}
Run Code Online (Sandbox Code Playgroud) 对于二叉树,我想获得落在单个垂直线中的所有节点的总和。我想要每个垂直节点中的节点总和
A
/ \
B C
/ \ / \
D E F G
/ \
H I
Run Code Online (Sandbox Code Playgroud)
如果你看上面的 T 恤
line 0 A E F so sum = A+E+F
line -1 B I so sum = B +I
line 1 C so sum = C
line 2 G so sum = G
Run Code Online (Sandbox Code Playgroud)
我实现了以下算法
Map<Integer,Integere> mp = new HashMap<Integer,Integer>()
calculate(root,0);
void calculate(Node node, int pos){
if(node==null)
return ;
if(mp.containsKey(pos) ){
int val = mp.get(pos) + node.data;
mp.put(pos,val);
}
else{
mp.put(pos,node.data); …Run Code Online (Sandbox Code Playgroud)