递归算法(从简到繁)

  • 递归:调用自身+递归出口

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

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

三. 斐波那契数列

前两项均为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);
        }
    }
}

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

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值