
树
文章平均质量分 56
枫流仁武
这个作者很懒,什么都没留下…
展开
-
LeetCode_173_二叉搜索树迭代器
实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器:BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。boolean hasNext() 如果向指针右侧遍历存在数字,则返回 true ;否则返回 false 。int next()将指针向右移动,然后返回指针处的数字。原创 2021-03-28 08:39:30 · 117 阅读 · 0 评论 -
LeetCode 951 翻转等价二叉树
我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树。只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y。编写一个判断两个二叉树是否是翻转等价的函数。这些树由根节点root1 和 root2给出。class Solution: def flipEquiv(self, root1: TreeNode, root2: TreeNode) -> bool: return self.jud..原创 2020-11-24 11:03:31 · 164 阅读 · 0 评论 -
LeetCode 222 完全二叉树的节点个数
给出一个完全二叉树,求出该树的节点个数。说明:完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~2h个节点。# Definition for a binary tree node.class TreeNode: def __init__(self, x): self.val = x self.left = None ..原创 2020-11-24 10:54:38 · 98 阅读 · 0 评论 -
LeetCode 834 树中距离之和
给定一个无向、连通的树。树中有 N 个标记为 0...N-1 的节点以及 N-1条边。第 i 条边连接节点edges[i][0] 和 edges[i][1]。返回一个表示节点 i 与其他所有节点距离之和的列表 ans。参考标准解析:链接写的答案。from typing import *class Solution: def __init__(self): self.ans = None self.num_nodes = None ...原创 2020-10-06 12:11:33 · 167 阅读 · 0 评论 -
LeetCode 701 二叉搜索树中的插入操作
给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据保证,新值和原始二叉搜索树中的任意节点值都不同。注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。class Solution: def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: if root is None:原创 2020-09-30 08:55:00 · 164 阅读 · 0 评论 -
LeetCode 145 二叉树的后序遍历
给定一个二叉树,返回它的后序遍历。官方给出的答案:from typing import *from collections import dequeclass Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: if not root: return [] res, stack = [], [] prev = Non..原创 2020-09-29 09:07:27 · 160 阅读 · 0 评论 -
LeetCode 538 把二叉树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和。中序遍历的变化版本from collections import dequeclass Solution: def convertBST(self, root: TreeNode) -> TreeNode: if root is None: return root原创 2020-09-21 07:49:31 · 135 阅读 · 0 评论 -
LeetCode 94 二叉树的中序遍历
可以利用递归和栈实现,栈实现的方法如下:class Solution: def inorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ stack = [] p = root res = [] while p or len(stack) != 0:原创 2020-09-14 08:19:39 · 66 阅读 · 0 评论 -
LeetCode 124 二叉树中的最大路径和
给定一棵非空二叉树,返回其最大路径和,路径为从任意节点出发,到达任意节点的序列,至少包含一个节点。本道题中路径可能弯折,因此首先要简化,考虑从某个节点往下搜索,路径和是多少,在考虑跨越该节点,最大路径和是多少。class Solution: def __init__(self): self.max_val=-sys.maxsize def maxPathSum(self, root: TreeNode) -> int: self.help原创 2020-06-21 15:36:57 · 141 阅读 · 0 评论 -
PAT 1135 Is It A Red-Black Tree
There is a kind of balanced binary search tree namedred-black treein the data structure. It has the following 5 properties:(1) Every node is either red or black. (2) The root is black. (3) Every...原创 2019-08-31 16:32:17 · 146 阅读 · 0 评论 -
PAT 1004 Counting Leaves
#include <iostream>#include <vector>#include <climits>#include <cstring>using namespace std;vector<int> vector1[105];//代表某个节点的叶子节点int nums[105];//代表每一层的叶子节点的个数int...原创 2019-07-22 15:42:20 · 91 阅读 · 0 评论 -
PAT 1099 Build A Binary Search Tree
建立二叉搜索树,id指的是在中序遍历过程中,对应节点的索引#include <iostream>#include <algorithm>#include <vector>#include <queue>/** * 建立二叉搜索树 * @return */ struct TreeNode{ int id; i...原创 2019-07-21 10:55:30 · 103 阅读 · 0 评论 -
1110 Complete Binary Tree
善于利用数组的索引机制,这个数组a1的下表代表着对应的值,而完全二叉树对应的数组没有显示地表现出来 当对应最大的索引是数字num总数减去1时,为完全二叉树#include <iostream>#include <cstring>using namespace std;struct node{ int left=-1; int right=-1;...原创 2019-07-20 20:15:34 · 117 阅读 · 0 评论 -
PAT 1021 Deepest Root
这道题第一遍做的时候没什么思路,现在再做感觉不难...没必要考虑太多,只有叶子节点才有可能成为最深的节点,只要判断所有叶子节点就可以了。注意有一个测试节点N为1,需要特殊考虑#include <iostream>#include <vector>#include <map>#include <cstring>using na...原创 2019-07-20 19:25:06 · 109 阅读 · 0 评论 -
1123 Is It a Complete AVL Tree
首先是建立二叉平衡树,然后是层序遍历,注意判断是否是完全树的条件:当发现某个节点的孩子为空时,以后如果发现某个节点存在孩子,那么就不是完全二叉树.构建二叉平衡树的过程:左左右旋,右右左旋,左右左旋左,右旋根,右左右旋右,左旋根,#include <iostream>#include <queue>using namespace std;...原创 2019-07-06 16:29:31 · 216 阅读 · 0 评论 -
PAT 1130 Infix Expression
#include <iostream>#include <map>#include <set>using namespace std;struct TreeNode{ string val; TreeNode* left; TreeNode* right;};TreeNode* root;void inOrder(Tree...原创 2019-07-04 14:03:06 · 82 阅读 · 0 评论 -
PAT LCA in a Binary Tree
这道题做过好多遍了,核心思想在于建立一个倒排索引,保存每个元素及其中序遍历对应的下标#include <iostream>#include <vector>#include <unordered_map>int preOrder[10005];int inOrder[10005];struct TreeNode{ int val; ...原创 2019-06-11 21:14:13 · 166 阅读 · 0 评论 -
PAT 1079 Total Sales of Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on...原创 2019-06-09 12:24:07 · 304 阅读 · 0 评论