温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java递归算法的经典实例

发布时间:2020-04-30 15:12:37 来源:亿速云 阅读:177 作者:小新 栏目:编程语言

今天小编给大家分享的是java递归算法的经典实例,相信很多人都不太了解,为了让大家更加了解java递归算法,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。

递归三要素:

1、明确递归终止条件;

2、给出递归终止时的处理办法;

3、提取重复的逻辑,缩小问题规模。

1、1+2+3+…+n

import java.util.Scanner;

public class Recursion {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.println(sum(n));
    }

    public static int sum(int n) {
        if(n == 1) {
            return n;
        }
        else {
            return n + sum(n-1);
        }
    }
}

2、1 * 2 * 3 * … * n

import java.util.Scanner;

public class Recursion {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.println(multiply(n));
    }

    public static int multiply(int n) {
        if(n == 1) {
            return n;
        }
        else {
            return n*multiply(n-1);
        }
    }
}

3、斐波那契数列

前两项均为1,第三项开始,每一项都等于前两项之和。即:1,1,2,3,5,8,…

import java.util.Scanner;

public class Recursion {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        System.out.println(fun(n));
    }

    public static int fun(int n) {

        if (n <= 2) {
            return 1;
        }
        else {
            return fun(n-1) + fun(n-2);
        }
    }
}

4、二叉树的遍历(前、中、后)

import java.util.Arrays;
import java.util.LinkedList;

public class MyBinaryTree {
    //二叉树节点
    private static class TreeNode{
        int data;
        TreeNode leftChild;
        TreeNode rightChile;

        public TreeNode(int data) {
            this.data = data;
        }
    }

    //构建二叉树
    public static TreeNode createBinaryTree(LinkedList<Integer> inputList) {
        TreeNode node = null;
        if(inputList == null || inputList.isEmpty()) {
            return null;
        }
        Integer data = inputList.removeFirst();

        //如果元素为空,则不再递归
        if(data != null){
            node = new TreeNode(data);
            node.leftChild = createBinaryTree(inputList);
            node.rightChile = createBinaryTree(inputList);
        }
        return node;
    }

    //前序遍历:根节点,左子树,右子树
    public static void preOrderTraveral(TreeNode node) {
        if (node == null) {
            return;
        }
        System.out.println(node.data);
        preOrderTraveral(node.leftChild);
        preOrderTraveral(node.rightChile);
    }

    //中序遍历:左子树,根节点,右子树
    public static void inOrderTraveral(TreeNode node) {
        if(node == null) {
            return;
        }

        inOrderTraveral(node.leftChild);
        System.out.println(node);
        inOrderTraveral(node.rightChile);

    }

    //后序遍历:左子树,右子树,根节点
    public static void postOrderTraveral(TreeNode node) {
        if (node == null) {
            return;
        }

        postOrderTraveral(node.leftChild);
        postOrderTraveral(node.rightChile);
        System.out.println(node.data);
    }

    public static void main(String[] args) {
       LinkedList<Integer> inputList = new LinkedList<Integer>(Arrays.asList(new Integer[]{3,2,9,null,null,10,null,null,8,null,4}));
       TreeNode treeNode = createBinaryTree(inputList);
       System.out.println("前序遍历:");
       preOrderTraveral(treeNode);

        System.out.println("中序遍历:");
        inOrderTraveral(treeNode);

        System.out.println("后序遍历:");
        postOrderTraveral(treeNode);
    }
}

以上就是java递归算法的经典实例的简略介绍,当然详细使用上面的不同还得要大家自己使用过才领会。如果想了解更多,欢迎关注亿速云行业资讯频道哦!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI