【视频】如何灵活运用递归?(Python/Java/C++/C/Go/JS/Rust)
看完这两期视频,让你对递归的理解更上一层楼!
答疑
问:代码中的 $-1$ 是怎么产生的?怎么返回的?
答:在某次递归中,发现左右子树高度绝对差大于 $1$,我们会返回 $-1$。这个 $-1$ 会一路向上不断返回,直到根节点。
写法一
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def get_height(node: Optional[TreeNode]) -> int:
if node is None:
return 0
left_h = get_height(node.left)
right_h = get_height(node.right)
if left_h == -1 or right_h == -1 or abs(left_h - right_h) > 1:
return -1
return max(left_h, right_h) + 1
return get_height(root) != -1
class Solution {
public boolean isBalanced(TreeNode root) {
return getHeight(root) != -1;
}
private int getHeight(TreeNode node) {
if (node == null) {
return 0;
}
int leftH = getHeight(node.left);
int rightH = getHeight(node.right);
if (leftH == -1 || rightH == -1 || Math.abs(leftH - rightH) > 1) {
return -1;
}
return Math.max(leftH, rightH) + 1;
}
}
class Solution {
int get_height(TreeNode* node) {
if (node == nullptr) {
return 0;
}
int left_h = get_height(node->left);
int right_h = get_height(node->right);
if (left_h == -1 || right_h == -1 || abs(left_h - right_h) > 1) {
return -1;
}
return max(left_h, right_h) + 1;
}
public:
bool isBalanced(TreeNode* root) {
return get_height(root) != -1;
}
};
#define MAX(a, b) ((b) > (a) ? (b) : (a))
int getHeight(struct TreeNode* node) {
if (node == NULL) {
return 0;
}
int left_h = getHeight(node->left);
int right_h = getHeight(node->right);
if (left_h == -1 || right_h == -1 || abs(left_h - right_h) > 1) {
return -1;
}
return MAX(left_h, right_h) + 1;
}
bool isBalanced(struct TreeNode* root) {
return getHeight(root) != -1;
}
func getHeight(node *TreeNode) int {
if node == nil {
return 0
}
leftH := getHeight(node.Left)
rightH := getHeight(node.Right)
if leftH == -1 || rightH == -1 || abs(leftH-rightH) > 1 {
return -1
}
return max(leftH, rightH) + 1
}
func isBalanced(root *TreeNode) bool {
return getHeight(root) != -1
}
func abs(x int) int { if x < 0 { return -x }; return x }
function getHeight(node) {
if (node === null) {
return 0;
}
const leftH = getHeight(node.left);
const rightH = getHeight(node.right);
if (leftH === -1 || rightH === -1 || Math.abs(leftH - rightH) > 1) {
return -1;
}
return Math.max(leftH, rightH) + 1;
}
var isBalanced = function(root) {
return getHeight(root) !== -1;
};
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
fn get_height(node: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
if let Some(node) = node {
let node = node.borrow();
let left_h = get_height(&node.left);
let right_h = get_height(&node.right);
if left_h == -1 || right_h == -1 || (left_h - right_h).abs() > 1 {
return -1;
}
return left_h.max(right_h) + 1;
}
0
}
get_height(&root) != -1
}
}
写法二
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def get_height(node: Optional[TreeNode]) -> int:
if node is None:
return 0
left_h = get_height(node.left)
if left_h == -1:
return -1 # 提前退出,不再递归
right_h = get_height(node.right)
if right_h == -1 or abs(left_h - right_h) > 1:
return -1
return max(left_h, right_h) + 1
return get_height(root) != -1
class Solution {
public boolean isBalanced(TreeNode root) {
return getHeight(root) != -1;
}
private int getHeight(TreeNode node) {
if (node == null) {
return 0;
}
int leftH = getHeight(node.left);
if (leftH == -1) {
return -1; // 提前退出,不再递归
}
int rightH = getHeight(node.right);
if (rightH == -1 || Math.abs(leftH - rightH) > 1) {
return -1;
}
return Math.max(leftH, rightH) + 1;
}
}
class Solution {
int get_height(TreeNode* node) {
if (node == nullptr) {
return 0;
}
int left_h = get_height(node->left);
if (left_h == -1) {
return -1; // 提前退出,不再递归
}
int right_h = get_height(node->right);
if (right_h == -1 || abs(left_h - right_h) > 1) {
return -1;
}
return max(left_h, right_h) + 1;
}
public:
bool isBalanced(TreeNode* root) {
return get_height(root) != -1;
}
};
#define MAX(a, b) ((b) > (a) ? (b) : (a))
int getHeight(struct TreeNode* node) {
if (node == NULL) {
return 0;
}
int left_h = getHeight(node->left);
if (left_h == -1) {
return -1; // 提前退出,不再递归
}
int right_h = getHeight(node->right);
if (right_h == -1 || abs(left_h - right_h) > 1) {
return -1;
}
return MAX(left_h, right_h) + 1;
}
bool isBalanced(struct TreeNode* root) {
return getHeight(root) != -1;
}
func getHeight(node *TreeNode) int {
if node == nil {
return 0
}
leftH := getHeight(node.Left)
if leftH == -1 {
return -1 // 提前退出,不再递归
}
rightH := getHeight(node.Right)
if rightH == -1 || abs(leftH-rightH) > 1 {
return -1
}
return max(leftH, rightH) + 1
}
func isBalanced(root *TreeNode) bool {
return getHeight(root) != -1
}
func abs(x int) int { if x < 0 { return -x }; return x }
function getHeight(node) {
if (node === null) {
return 0;
}
const leftH = getHeight(node.left);
if (leftH === -1) {
return -1; // 提前退出,不再递归
}
const rightH = getHeight(node.right);
if (rightH === -1 || Math.abs(leftH - rightH) > 1) {
return -1;
}
return Math.max(leftH, rightH) + 1;
}
var isBalanced = function(root) {
return getHeight(root) !== -1;
};
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
fn get_height(node: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
if let Some(node) = node {
let node = node.borrow();
let left_h = get_height(&node.left);
if left_h == -1 {
return -1; // 提前退出,不再递归
}
let right_h = get_height(&node.right);
if right_h == -1 || (left_h - right_h).abs() > 1 {
return -1;
}
return left_h.max(right_h) + 1;
}
0
}
get_height(&root) != -1
}
}
复杂度分析
- 时间复杂度:$\mathcal{O}(n)$,其中 $n$ 为二叉树的节点个数。
- 空间复杂度:$\mathcal{O}(n)$。最坏情况下,二叉树退化成一条链,递归需要 $\mathcal{O}(n)$ 的栈空间。
分类题单
- 滑动窗口与双指针(定长/不定长/单序列/双序列/三指针/分组循环)
- 二分算法(二分答案/最小化最大值/最大化最小值/第K小)
- 单调栈(基础/矩形面积/贡献法/最小字典序)
- 网格图(DFS/BFS/综合应用)
- 位运算(基础/性质/拆位/试填/恒等式/思维)
- 图论算法(DFS/BFS/拓扑排序/基环树/最短路/最小生成树/网络流)
- 动态规划(入门/背包/划分/状态机/区间/状压/数位/数据结构优化/树形/博弈/概率期望)
- 常用数据结构(前缀和/差分/栈/队列/堆/字典树/并查集/树状数组/线段树)
- 数学算法(数论/组合/概率期望/博弈/计算几何/随机算法)
- 贪心与思维(基本贪心策略/反悔/区间/字典序/数学/思维/脑筋急转弯/构造)
- 链表、二叉树与回溯(前后指针/快慢指针/DFS/BFS/直径/LCA/一般树)
- 字符串(KMP/Z函数/Manacher/字符串哈希/AC自动机/后缀数组/子序列自动机)
欢迎关注 B站@灵茶山艾府
