1 2 3 4

递归算法

上一层的函数还没执行完就调用下一层,因此,当达到递归终止条件时,首先return的是最底层调用的函数
int Sum(int n)
{
    if (n <= 1)                  //#1
        return n;                //#2
    return n+Sum(n - 1);         //#3
}
若n=10,则return 10 + Sum(9),接着return 10 + 9 + Sum(8)... 
当n=1时,最底层return 1 给倒数第二层,
倒数第二层return 2 + 1 给倒数第三层...
最后返回的结果就是 10 + 9 + 8 + ... + 1 = 55

java递归算法遍历二叉树:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public class TreeNode {
    public int data;
    public TreeNode leftChild;
    public TreeNode rightChild;<br><br>
    public TreeNode(int data) {
        this.data = data;
    }<br>
    /**
     * 创建二叉树
     *
     * @param list
     * @return
     */
    public static TreeNode createBinaryTree(LinkedList<Integer> list) {
        TreeNode node = null;
        if (list == null || list.isEmpty()) {
            return null;
        }
        Integer data = list.removeFirst();
        if (data != null) {
            node = new TreeNode(data);
            node.leftChild = createBinaryTree(list);
            node.rightChild = createBinaryTree(list);
        }
        return node;
    }
 
    public static void main(String[] args) {
 
        LinkedList<Integer> inputList1 = new LinkedList<>
                (Arrays.asList(1, 2, 3, null, null, 4, 5, null, 6, null, null, 7, null, null));
        TreeNode treeNode = createBinaryTree(inputList1);
        System.out.println("前序遍历:");
        preOrderTraveral(treeNode);
        System.out.println("\n" + "中序遍历:");
        inOrderTraveral(treeNode);
        System.out.println("\n" + "后序遍历:");
        postOrderTraveral(treeNode);
 
    }
 
 
    /**
     * 前序遍历
     *
     * @param node
     */
    public static void preOrderTraveral(TreeNode node) {
        if (node == null) {
            return;
        }
        System.out.print(node.data + " ");
        preOrderTraveral(node.leftChild);
        preOrderTraveral(node.rightChild);
 
    }
 
 
    /**
     * 中序遍历
     *
     * @param node
     */
    public static void inOrderTraveral(TreeNode node) {
        if (node == null) {
            return;
        }
        inOrderTraveral(node.leftChild);
        System.out.print(node.data + " ");
        inOrderTraveral(node.rightChild);
 
    }
 
 
    /**
     * 后续遍历
     *
     * @param node
     */
    public static void postOrderTraveral(TreeNode node) {
        if (node == null) {
            return;
        }
        postOrderTraveral(node.leftChild);
        postOrderTraveral(node.rightChild);
        System.out.print(node.data + " ");
 
    }
}

  




posted @   日月星宿  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示