上一页 1 2 3 4 5 6 7 8 9 ··· 13 下一页
摘要: 问题描述 https://leetcode.cn/problems/validate-binary-search-tree/description/ 解题思路 二叉搜索树的性质是:root节点要大于左子树的最大值,小于右子树的最小值。 同时,对上层,也要更新本层的最大值和最小值以供继续递归。 补充: 阅读全文
posted @ 2023-01-29 11:13 BJFU-VTH 阅读(19) 评论(0) 推荐(0) 编辑
摘要: 问题描述 https://leetcode.cn/problems/binary-tree-paths/description/ 解题思路 叶子结点时,添加到结果序列即可。 代码 # Definition for a binary tree node. # class TreeNode: # def 阅读全文
posted @ 2023-01-28 19:41 BJFU-VTH 阅读(16) 评论(0) 推荐(0) 编辑
摘要: 问题描述 https://leetcode.cn/problems/invert-binary-tree/description/ 解题思路 没啥好说的,python的交换简单极了。 代码 # Definition for a binary tree node. # class TreeNode: 阅读全文
posted @ 2023-01-28 17:50 BJFU-VTH 阅读(13) 评论(0) 推荐(0) 编辑
摘要: 问题描述 https://leetcode.cn/problems/binary-tree-postorder-traversal/description/ 解题思路 这个题和先序一样,没啥好说的。 代码 # Definition for a binary tree node. # class Tr 阅读全文
posted @ 2023-01-28 17:46 BJFU-VTH 阅读(9) 评论(0) 推荐(0) 编辑
摘要: 问题描述 https://leetcode.cn/problems/binary-tree-preorder-traversal/description/ 解题思路 二叉树的先序遍历,没啥好说的。中-左-右。 先序中序后序 说的是中在哪里。 代码 # Definition for a binary 阅读全文
posted @ 2023-01-28 17:40 BJFU-VTH 阅读(12) 评论(0) 推荐(0) 编辑
摘要: 问题描述 https://leetcode.cn/problems/path-sum/description/ 解题思路 我们可以对叶子结点进行判断,如果叶子结点的值等于targetSum,那么就算是找到了。 代码 # Definition for a binary tree node. # cla 阅读全文
posted @ 2023-01-28 17:35 BJFU-VTH 阅读(14) 评论(0) 推荐(0) 编辑
摘要: 问题描述 https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/ 解题思路 这个题目不难,但对退出条件要求高。 经过对题意的分析,我们对于root为None的情况,以及root为叶子结点的情况,以及root只有左子 阅读全文
posted @ 2023-01-28 16:24 BJFU-VTH 阅读(15) 评论(0) 推荐(0) 编辑
摘要: 问题描述 https://leetcode.cn/problems/balanced-binary-tree/description/ 解题思路 这题一开始朴素的思路就是,对于每个节点,都计算其是不是平衡二叉树。 计算平衡二叉树的方式是对其求高度。 这会导致我们搜索了2次。 然而我们可以在一次搜索中 阅读全文
posted @ 2023-01-28 15:56 BJFU-VTH 阅读(9) 评论(0) 推荐(0) 编辑
摘要: 问题描述 https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/ 解题思路 二叉树的最大深度,等于左子树的深度和右子树深度的较大值加1(即本层深度). 代码 # Definition for a binary tr 阅读全文
posted @ 2023-01-28 15:32 BJFU-VTH 阅读(16) 评论(0) 推荐(0) 编辑
摘要: 问题描述 https://leetcode.cn/problems/symmetric-tree/description/ 解题思路 这个题,一看就是递归。既然如此,我们按照递归的一般思路来看,即问题的定义即为问题的解。 这个题目看似复杂,实际就是在求左子树的左孩子等于右子树的右孩子,左子树的右孩子 阅读全文
posted @ 2023-01-28 15:21 BJFU-VTH 阅读(13) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 ··· 13 下一页