最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java遞歸算法的實(shí)例詳解

 更新時(shí)間:2020年02月05日 09:48:09   作者:V  
在本篇文章里小編給大家整理了關(guān)于java遞歸算法的實(shí)例內(nèi)容,以及相關(guān)知識(shí)點(diǎn)總結(jié),需要的朋友們可以學(xué)習(xí)下。

遞歸三要素:

1、明確遞歸終止條件;

2、給出遞歸終止時(shí)的處理辦法;

3、提取重復(fù)的邏輯,縮小問(wèn)題規(guī)模。

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、斐波那契數(shù)列

前兩項(xiàng)均為1,第三項(xiàng)開(kāi)始,每一項(xiàng)都等于前兩項(xiàng)之和。即: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、二叉樹(shù)的遍歷(前、中、后)

import java.util.Arrays;

import java.util.LinkedList;

 

public class MyBinaryTree {

  //二叉樹(shù)節(jié)點(diǎn)

  private static class TreeNode{

    int data;

    TreeNode leftChild;

    TreeNode rightChile;

 

    public TreeNode(int data) {

      this.data = data;

    }

  }

 

  //構(gòu)建二叉樹(shù)

  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;

  }

 

  //前序遍歷:根節(jié)點(diǎn),左子樹(shù),右子樹(shù)

  public static void preOrderTraveral(TreeNode node) {

    if (node == null) {

      return;

    }

    System.out.println(node.data);

    preOrderTraveral(node.leftChild);

    preOrderTraveral(node.rightChile);

  }

 

  //中序遍歷:左子樹(shù),根節(jié)點(diǎn),右子樹(shù)

  public static void inOrderTraveral(TreeNode node) {

    if(node == null) {

      return;

    }

 

    inOrderTraveral(node.leftChild);

    System.out.println(node);

    inOrderTraveral(node.rightChile);

 

  }

 

  //后序遍歷:左子樹(shù),右子樹(shù),根節(jié)點(diǎn)

  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遞歸算法實(shí)例的詳細(xì)內(nèi)容,大家如果有任何補(bǔ)充的地方可以聯(lián)系腳本之家小編。

相關(guān)文章

最新評(píng)論

佳木斯市| 镇安县| 双峰县| 武乡县| 五华县| 扎鲁特旗| 大关县| 如皋市| 龙游县| 泰兴市| 海南省| 洱源县| 永川市| 交口县| 永宁县| 龙陵县| 丽水市| 重庆市| 阜新市| 孟州市| 宜兰县| 图木舒克市| 泰兴市| 龙岩市| 平谷区| 萝北县| 梁河县| 都兰县| 灯塔市| 连云港市| 松江区| 搜索| 施秉县| 九龙坡区| 民权县| 宁明县| 舞阳县| 湖州市| 万荣县| 乐山市| 广州市|