代码怎么递归树形结构对应的值呢

J2EE 码拜 9年前 (2015-04-02) 949次浏览 0个评论
 

代码怎么递归树形结构对应的值呢

如图,怎么根据前2列,得出递归的值呢?

代码怎么递归树形结构对应的值呢
40分
直接上代码:

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);
		}

	}
}

结果:
代码怎么递归树形结构对应的值呢

代码怎么递归树形结构对应的值呢
思路
第n层的值an
第n-1层递归的值Yn-1
那么第n层递归值Yn = an * Yn-1

其实感觉这个应该是排列组合的知识,用不着递归,直接遍历“层级对应的值”就可以了

层级对应的值可以存Map<Integer,Integer> map中,循环的时候i=0;i<map.size;i++即可。

代码怎么递归树形结构对应的值呢
引用 1 楼 ttjxtjx 的回复:

直接上代码:

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);
		}

	}
}

结果:
代码怎么递归树形结构对应的值呢

看到不是特别明白,能否用for循环,然后变量去做呢?

代码怎么递归树形结构对应的值呢
引用 3 楼 yang3088850111he 的回复:
Quote: 引用 1 楼 ttjxtjx 的回复:

直接上代码:

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);
		}

	}
}

结果:
代码怎么递归树形结构对应的值呢

看到不是特别明白,能否用for循环,然后变量去做呢?

构造节点代码应该看的懂吧。你前面说要递归的,我改了下递归或者for循环都可以的,结果是一样的。

package test;
 
import java.util.ArrayList;
import java.util.List;
 
public class Main {
     
    public static int mul(Node n,int total){
    	//root节点返回
        if(n==Node.root){
            return total;
        }else{
        	//非root节点,递归。
            return total*mul(n.parent,n.value);
        }
    }
    
    public static int mul(Node n){
    	int total = 1;
    	for(;;n=n.parent){
    		//root节点返回
    		if(n==null){
    			break;
    		}else{
    			total*=n.value;
    		}
    	}
    	return total;
    }
     
    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));
        System.out.println(mul(n1));
        System.out.println(mul(n21));
        System.out.println(mul(n22));
        System.out.println(mul(n3));
    }
     
    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);
        }
         
    }
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明代码怎么递归树形结构对应的值呢
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!