如图,怎么根据前2列,得出递归的值呢?
------解决思路----------------------
直接上代码:
package test;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static int mul(Node n,int total){
if(n.lever==0){
return total;
}else{
return total*mul(n.parent,n.value);
}
}
public static void main(String[] args) {
Node n1 = new Node(3,Node.root);
Node n21 = new Node(2,n1);
Node n22 = new Node(3,n1);
Node n3 = new Node(5,n22);
System.out.println(mul(n1,1));
System.out.println(mul(n21,1));
System.out.println(mul(n22,1));
System.out.println(mul(n3,1));
}
public static class Node{
int lever;
int value;
List<Node> child = new ArrayList<Node>();
Node parent;
static final Node root = new Node(0, 1);
private Node(int lever, int value) {
super();
this.lever = lever;
this.value = value;
parent = null;
}
public Node( int value,Node parent) {
super();
this.lever = parent.lever+1;
this.value = value;
this.parent = parent;
parent.child.add(this);
}
}
}
结果: