Maximum Depth of Binary Tree

本文详细阐述了使用递归方法解决二叉树最大深度问题的算法实现,通过定义辅助函数和逐步深入节点的方式,有效计算出二叉树的最大深度。文章深入浅出地解释了算法原理,提供了易于理解的代码示例。
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int max = -1;
    int maxDepth(TreeNode *root ) {
        if( root == NULL )
            return 0;
        return findMax( root, 1 );
    }
    int findMax( TreeNode *root, int val )
    {
        if( root == NULL )
            return 0;
        if( val > max )
            max = val;
        findMax( root->left, val+1 );
        findMax( root->right, val+1 );
        return max;
    }
};

nsert 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty AVL tree. Which one of the following statements is FALSE? A. 4 is the root B. 3 and 7 are siblings C. 2 and 6 are siblings D. 9 is the parent of 7 分数 3 作者 陈越 单位 浙江大学 如果AVL树的深度为5(空树的深度定义为0),则此树最少有多少个结点? A. 12 B. 20 C. 33 D. 64 分数 2 作者 徐镜春 单位 浙江大学 将 7, 8, 9, 2, 3, 5, 6, 4 顺序插入一棵初始为空的AVL树。下列句子中哪句是错的? A. 7 是根结点 B. 2 和 5 是兄弟 C. 有2个结点的平衡因子为-1 D. 3 是 4 的父结点 分数 2 作者 何钦铭 单位 浙江大学 For an AVL-tree of height 4, what is the minimum number of the tree nodes? (the height of one node tree is 0) A. 11 B. 12 C. 14 D. 15 分数 2 作者 何钦铭 单位 浙江大学 During inserting { 42, 26, 8, 70, 102, 56, 2} into an initially empty AVL tree, which of the following statements is true? A. The resulted AVL tree is also a completed binary tree; B. There are 4 rotations: LL,RR,LR,LL C. There are 4 rotations: LL,RR,LL,RL D. There are 3 rotations: LL,RR,RL 分数 2 作者 何钦铭 单位 浙江大学 When inserting 1, 2, 3, 6, 5, and 4 one by one into an initially empty AVL tree,which kinds of rotations will be encountered? A. Two RR's and one RL B. One RR, one RL, and one LR C. One RR and two RL's D. Two RR's and one LR 分数 2 作者 陈越 单位 浙江大学 将 26, 13, 44, 51, 98, 37, 66, 73 顺序插入一棵初始为空的AVL树。下列句子中哪句是错的? A. 44 是根结点 B. 37 和 73 是兄弟 C. 26 和 66 是兄弟 D. 26 是 13 的父结点 分数 2 作者 何钦铭 单位 浙江大学 将一系列数字顺序一个个插入一棵初始为空的AVL树。下面哪个系列的第一次旋转是“右-左”双旋? A. 1,2,3,4,5,6 B. 6,5,4,3,2,1 C. 4,2,5,6,3,1 D. 3,1,4,6,5,2 分数 2 作者 考研真题 单位 浙江大学 Delete a node v from an AVL tree T1​, we can obtain another AVL tree T2​. Then insert v into T2​, we can obtain another AVL tree T3​. Which one(s) of the following statements about T1​ and T3​ is(are) true? I、If v is a leaf node in T1​, then T1​ and T3​ might be different. II、If v is not a leaf node in T1​, then T1​ and T3​ must be different. III、If v is not a leaf node in T1​, then T1​ and T3​ must be the same. A. I only B. II only C. I and II only D. I and III only 分数 2 作者 何钦铭 单位 浙江大学 Insert 28, 23, 54, 61, 98, 37 into an initially empty AVL tree first. Then immediately insert one of the following keys. Which one will cause an RL rotation? A. 10 B. 30 C. 60 D. 70 分数 2 作者 何钦铭 单位 浙江大学 Insert 28, 23, 54, 61, 98, 37 into an initially empty AVL tree first. Then immediately insert one of the following keys. Which one will cause an RL rotation? A. 10 B. 50 C. 80 D. 100 分数 3 作者 陈越 单位 浙江大学 If there are 14 nodes in an AVL tree, then the maximum depth of the tree is ____. The depth of an empty tree is defined to be 0. A. 3 B. 4 C. 5 D. 6 分数 3 作者 陈越 单位 浙江大学 If there are 28 nodes in an AVL tree, then the maximum depth of the tree is ____. The depth of an empty tree is defined to be -1. A. 3 B. 4 C. 5 D. 6 分数 3 作者 DS课程组 单位 浙江大学 If the depth of an AVL tree is 6 (the depth of an empty tree is defined to be -1), then the minimum possible number of nodes in this tree is: A. 13 B. 17 C. 20 D. 33 分数 2 作者 101 数据结构团队 单位 101计划 给定关键词输入序列 { CAP, AQU, PIS, ARI, TAU, GEM, CAN, LTB, VIR, LEO, SCO },,试按表中元素的次序,依次插入一棵初始为空的 AVL 树(字符之间以字典顺序比较大小)。则关于最后产生的 AVL 树,哪句描述是错误的? A. 在形成结果树的过程中,涉及 2 次旋转调整 B. LTB 是 GEM 的父结点 C. PIS 是 TAU 的父结点 D. CAP 是根结点
10-11
以下是针对不同类型AVL树相关选择题的解答分析: ### 插入特定数字序列到AVL树后的节点关系判断 插入操作和二分搜索树类似,但需多考虑节点的平衡问题。若一串数字插入顺序为3,4,5,树结构会退化为链表,此时AVL树会进行旋转操作以达到平衡,旋转操作会在增加、删除、修改时进行,旋转分为LL型、LR型、RR型和RL型四种情况 [^2][^3]。判断节点关系时,要先根据插入顺序构建AVL树,插入过程中若不满足平衡条件(每个结点左右子树高度之差的绝对值不超过1),就需进行相应旋转操作,最终根据旋转后的树结构判断节点关系。 ### AVL树特定深度下的最少节点数计算 设AVL树深度为 $d$,最少节点数为 $N(d)$。深度为0时,$N(0) = 0$;深度为1时,$N(1) = 1$。对于 $d > 1$,$N(d) = N(d - 1) + N(d - 2) + 1$,这是因为AVL树要达到最少节点数,其左右子树高度差为1,左右子树分别是深度为 $d - 1$ 和 $d - 2$ 的AVL树,再加上根节点。例如,求深度为3的AVL树最少节点数,$N(2)=N(1)+N(0)+1 = 1 + 0 + 1 = 2$,$N(3)=N(2)+N(1)+1 = 2 + 1 + 1 = 4$。 ### 插入节点导致的旋转类型判断 失衡条件分为LL型、LR型、RR型和RL型四种,针对不同类型的失衡有不同的旋转策略 [^3]。 - **LL型**:在节点的左子树的左子树上插入新节点,导致树失衡,需进行右旋操作。 - **LR型**:在节点的左子树的右子树上插入新节点,导致树失衡,需先对左子树进行左旋,再对根节点进行右旋。 - **RR型**:在节点的右子树的右子树上插入新节点,导致树失衡,需进行左旋操作。 - **RL型**:在节点的右子树的左子树上插入新节点,导致树失衡,需先对右子树进行右旋,再对根节点进行左旋。 ### 代码示例 ```python # 定义AVL树节点类 class TreeNode: def __init__(self, key): self.key = key self.left = None self.right = None self.height = 1 # 定义AVL树类 class AVLTree: def insert(self, root, key): if not root: return TreeNode(key) elif key < root.key: root.left = self.insert(root.left, key) else: root.right = self.insert(root.right, key) root.height = 1 + max(self.get_height(root.left), self.get_height(root.right)) balance = self.get_balance(root) # LL型 if balance > 1 and key < root.left.key: return self.right_rotate(root) # RR型 if balance < -1 and key > root.right.key: return self.left_rotate(root) # LR型 if balance > 1 and key > root.left.key: root.left = self.left_rotate(root.left) return self.right_rotate(root) # RL型 if balance < -1 and key < root.right.key: root.right = self.right_rotate(root.right) return self.left_rotate(root) return root def left_rotate(self, z): y = z.right T2 = y.left y.left = z z.right = T2 z.height = 1 + max(self.get_height(z.left), self.get_height(z.right)) y.height = 1 + max(self.get_height(y.left), self.get_height(y.right)) return y def right_rotate(self, z): y = z.left T3 = y.right y.right = z z.left = T3 z.height = 1 + max(self.get_height(z.left), self.get_height(z.right)) y.height = 1 + max(self.get_height(y.left), self.get_height(y.right)) return y def get_height(self, root): if not root: return 0 return root.height def get_balance(self, root): if not root: return 0 return self.get_height(root.left) - self.get_height(root.right) # 示例使用 avl = AVLTree() root = None keys = [3, 4, 5] for key in keys: root = avl.insert(root, key) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值