递归返回值使用模式总结
递归返回值的核心作用递归返回值主要有以下几种使用模式:1. 传递结果型返回值特点:返回值直接就是最终结果场景示例:// 二叉树的最大深度 public int maxDepth(TreeNode root) { if (root == null) return 0; int left = maxDepth(root.left); int right = maxDepth(root.right); return Math.max(left, right) + 1; // 返回值就是深度结果 } // 斐波那契数列 public int fibonacci(...
最近评论