재귀의 세 가지 요소:
1. 재귀 종료 조건을 명확히 합니다.
2. 반복되는 논리를 추출하고 문제의 크기를 줄입니다.
1, 1+2+3+…+nimport 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);
}
}
}
(권장 학습:
java 비디오 튜토리얼)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); } } }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);
}
}
관련 기사 튜토리얼 공유:
위 내용은 자바 재귀 알고리즘 예제의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

DVWA
DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.
