算法D16 | 二叉树3 | 104.二叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数

迭代法,大家可以直接过,二刷有精力的时候 再去掌握迭代法。

104.二叉树的最大深度 (优先掌握递归)

什么是深度,什么是高度,如何求深度,如何求高度,这里有关系到二叉树的遍历方式。

大家 要先看视频讲解,就知道以上我说的内容了,很多录友刷过这道题,但理解的还不够。

题目链接/文章讲解/视频讲解: 代码随想录

二叉树深度和高度是不同的概念,顺序是相反的,高度从叶子往根部递增,深度是从根部往叶子递增;求深度用的是前序遍历,求高度用的是后序遍历。

二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)

这个题因为刚好最大深度==高度。深度高度均从0开始。

Python递归:

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root: return 0
        result = 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
        return result

# 真正的过程拆解,可以写清楚前序遍历或者后序遍历。
# 用后序遍历求高度
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root: return 0
        left_height = self.maxDepth(root.left)   # 左
        right_height = self.maxDepth(root.right) # 右
        height = 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) # 中
        return height

C++递归:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root==NULL) return 0;
        int result = 1 + max(maxDepth(root->left), maxDepth(root->right)); 
        return result;
    }
};

拓展:559.n叉树的最大深度

Python递归:

class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if not root: return 0
        depth = 0
        for child in root.children:
            depth = max(depth, self.maxDepth(child))
        return depth+1

C++递归:

class Solution {
public:
    int maxDepth(Node* root) {
        if (root==NULL) return 0;
        int depth = 0;
        if (root!=NULL) {
            for (int i=0; i<root->children.size(); i++) {
                depth = max(depth, maxDepth(root->children[i]));
            }
        }
        return depth+1;
    }
};

111.二叉树的最小深度 (优先掌握递归)

先看视频讲解,和最大深度 看似差不多,其实 差距还挺大,有坑。

题目链接/文章讲解/视频讲解:代码随想录

这里要注意深度是从叶子往根部访问,需要考虑的是有叶的边,如果左边没有叶子,那么考虑右边,反之亦然。仍然可以用后序遍历实现,代码较简单。

Python递归:

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root: return 0
        left_height = self.minDepth(root.left)
        right_height = self.minDepth(root.right)
        if left_height == 0:
            height = 1 + right_height
        elif right_height == 0:
            height = 1 + left_height
        else:
            height = 1 + min(left_height, right_height)
        return height

C++递归:

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root==NULL) return 0;
        int left_height = minDepth(root->left);
        int right_height = minDepth(root->right);
        int height;
        if (left_height==0) {
            height = 1 + right_height;
        } else if (right_height==0) {
            height = 1 + left_height;
        } else {
            height = 1 + min(left_height, right_height);
        }
        return height;
    }
};

222.完全二叉树的节点个数(优先掌握递归)

需要了解,普通二叉树 怎么求,完全二叉树又怎么求

题目链接/文章讲解/视频讲解:代码随想录

Python递归:

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if not root: return 0
        left_node = self.countNodes(root.left)
        right_node = self.countNodes(root.right)
        result = 1 + left_node + right_node
        return result
        

C++递归:

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root==NULL) return 0;
        int left_node = countNodes(root->left);
        int right_node = countNodes(root->right);
        int result = 1 + left_node + right_node;
        return result;
    }
};

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据你提供的引用内容,你遇到的错误是"Uncaught SyntaxError: The requested module '/node_modules/.vite/deps/pinia-plugin-persist.js?v=ca9d16b3' does not provide an export named 'pinaPluginPersist'"。这个错误通常是由于模块导出的名称不匹配导致的。 根据你提供的引用内容,我注意到你在Vue 3中使用了新的导入语法`import * as VueRouter from 'vue-router'`,而不是Vue 2中的`import Router from 'vue-router'`。这是因为在Vue 3中,Vue Router的导入方式发生了变化。 另外,你还提供了Vue Router的配置代码。根据你的代码,你使用了`createRouter`和`createWebHashHistory`来创建路由实例,并配置了路由规则。这些配置在Vue 3中是正确的。 然而,根据你提供的引用内容,你遇到的错误与Vue Router的配置无关。这个错误是关于一个名为'pinaPluginPersist'的导出名称的问题。根据错误信息,它指出请求的模块没有提供名为'pinaPluginPersist'的导出。 要解决这个错误,你可以检查你的代码中是否有使用'pinaPluginPersist'这个导出名称的地方。确保你正确导入和使用了这个模块,并且导出名称是正确的。如果你不确定正确的导出名称是什么,可以查看相关的文档或示例代码来获取正确的导出名称。 总结一下,你遇到的错误是由于请求的模块没有提供名为'pinaPluginPersist'的导出。你可以检查你的代码中是否正确导入和使用了这个模块,并确保导出名称是正确的。如果你需要进一步的帮助,请提供更多的代码和错误信息。 #### 引用[.reference_title] - *1* *2* [Uncaught SyntaxError: The requested module ‘/node_modules/.vite/deps/vue-router.js?v=0d658a16‘ do....](https://blog.csdn.net/weixin_55939638/article/details/129715463)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值