![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode树
肉丸不肉
这个作者很懒,什么都没留下…
展开
-
树专题经典题目+方法总结
一、二叉树(Binary Tree)1.1 二叉树的遍历题目(前序、后序、中序、层次)后序遍历 = 删除树中结点当你删除一个节点时,你将首先删除它的左节点和它的右边的节点,然后再删除节点本身。层次遍历使用 BFS 实现,利用的就是 BFS 一层一层遍历的特性;而前序、中序、后序遍历利用了 DFS 实现。前中后序遍历的三种方法:递归、迭代、颜色标记法层次遍历方法:使用 BFS 进行层...原创 2020-03-12 17:47:13 · 899 阅读 · 0 评论 -
LeetCode 677. Map Sum Pairs(键值映射)
Implement a MapSum class with insert, and sum methods.For the method insert, you'll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the k...原创 2020-03-12 17:02:10 · 152 阅读 · 0 评论 -
LeetCode 208. Implement Prefix Tree(实现前缀树)
Implement a trie with insert, search, and startsWith methods.Example:Trie trie = new Trie();trie.insert("apple");trie.search("apple"); // returns truetrie.search("app"); // returns false...原创 2020-03-12 16:51:29 · 115 阅读 · 0 评论 -
前缀树
一、什么是前缀树?前缀树是N叉树的一种特殊形式。通常来说,一个前缀树是用来存储字符串的。前缀树的每一个节点代表一个字符串(前缀)。每一个节点会有多个子节点,通往不同子节点的路径上有着不同的字符。子节点代表的字符串是由节点本身的原始字符串,以及通往该子节点路径上所有的字符组成的。下面是前缀树的一个例子:在上图示例中,我们在节点中标记的值是该节点对应表示的字符串。例如,我们从根节点开始,选择第...原创 2020-03-11 17:28:13 · 400 阅读 · 0 评论 -
LeetCode 501. Find Mode in Binary Search Tree(二叉搜索树中的众数集)
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.For example:Given BST [1,null,2,2], 1 \ 2 / 2 ...原创 2020-03-11 16:58:30 · 169 阅读 · 0 评论 -
LeetCode 530. Minimum Absolute Difference in BST(二叉搜索树的最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.Example:Input: 1 \ 3 / 2Output:1Explanation:The ...原创 2020-03-11 16:45:04 · 174 阅读 · 0 评论 -
653. Two Sum IV - Input is a BST(两数之和,输入为BST)
//中序遍历-递增序列,双指针 List<Integer> res = new ArrayList<>(); public boolean findTarget(TreeNode root, int k) { if(root == null) return false; inOrder(root);...原创 2020-03-11 16:35:01 · 178 阅读 · 0 评论 -
LeetCode 109. Convert Sorted List to Binary Search Tree(有序链表转换为二叉搜索树)
public TreeNode sortedListToBST(ListNode head) { //链表没有结点 if(head == null) return null; //链表只有一个结点 if(head.next == null) return new TreeNode(head.val); //找...原创 2020-03-11 16:23:36 · 156 阅读 · 0 评论 -
LeetCode 538. Convert BST to Greater Tree(把二叉搜索树转换为累加树)
Example:Input: The root of a Binary Search Tree like this: 5 / \ 2 13Output: The root of a Greater Tree like this: 18 / \ ...原创 2020-03-11 15:58:57 · 176 阅读 · 0 评论 -
LeetCode 230. Kth Smallest Element in a BST(二叉搜索树中第K小的元素)
public int kthSmallest(TreeNode root, int k) { //左子树结点个数 int leftNum = nodeNum(root.left); if(leftNum == k - 1) { return root.val; } //是大于k-1,而不...原创 2020-03-11 15:45:02 · 180 阅读 · 0 评论 -
LeetCode 669. Trim a Binary Search Tree(修剪二叉搜索树)
all its elements lies in [L, R] (R >= L). public TreeNode trimBST(TreeNode root, int L, int R) { if(root == null) return root; //两个if是删除不满足要求的点 //修剪后的二叉树必定出现在节点的右边。 ...原创 2020-03-11 15:24:56 · 167 阅读 · 0 评论 -
LeetCode 513. Find Bottom Left Tree Value(找树左下角的值)
Given a binary tree, find the leftmost value in the last row of the tree.Example 1:Input: 2 / \ 1 3Output:1Example 2: Input: 1 / \ 2 3 / / \ 4 ...原创 2020-03-11 15:00:06 · 197 阅读 · 0 评论 -
LeetCode 637. Average of Levels in Binary Tree(一棵树每层节点的平均数)
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Example 1:Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanation:...原创 2020-03-11 14:41:21 · 219 阅读 · 0 评论 -
LeetCode 108. Convert Sorted Array to Binary Search Tree(将有序数组转换为高度平衡的二叉搜索树)
根据中序遍历建树 不唯一 //根据中序遍历序列创建二叉搜索树 public TreeNode sortedArrayToBST(int[] nums) { if(nums.length == 0) return null; return helper(nums, 0, nums.length - 1); } public Tre...原创 2020-03-10 17:49:05 · 165 阅读 · 0 评论 -
LeetCode 703. Kth Largest Element in a Stream(数据流中第K大元素)
设计一个找到数据流中第K大元素的类(class)。注意是排序后的第K大元素,不是第K个不同的元素。你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包含数据流中的初始元素。每次调用 KthLargest.add,返回当前数据流中第K大的元素。示例:int k = 3;int[] arr = [4,5,8,2];KthLargest kthLar...原创 2020-03-10 17:17:53 · 261 阅读 · 0 评论 -
LeetCode 700+701+450题目-BST操作:搜索、插入、删除
一、搜索递归 public TreeNode searchBST(TreeNode root, int val) { //是return root,而不是return null if(root == null) return root; if(root.val == val) return root; if(root....原创 2020-03-10 15:58:46 · 291 阅读 · 0 评论 -
LeetCode 173. Binary Search Tree Iterator(BST迭代器)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST. Example:B...原创 2020-03-09 21:10:11 · 245 阅读 · 0 评论 -
LeetCode 98. Validate Binary Search Tree(验证BST)
Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.(节点的左子树只包含**小于**当前节点的数。)The right subtree of a node contains only nodes with key...原创 2020-03-09 20:44:20 · 213 阅读 · 0 评论 -
LeetCode 671. Second Minimum Node In a Binary Tree(二叉树中第二小的节点)
给定一个非空特殊的二叉树,每个节点都是正数,并且**每个节点的子节点数量只能为 2 或 0**。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。 root.val = min(root.left.val, root.right.val)给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。示例 1:输入: 2 ...原创 2020-03-08 23:23:42 · 205 阅读 · 0 评论 -
House Robber(打家劫舍问题)
待补充原创 2020-03-08 22:57:43 · 351 阅读 · 0 评论 -
LeetCode 687. Longest Univalue Path(最长同值路径)
Given a binary tree, find the length of the longest path where each node in the path has the same value. **This path may or may not pass through the root.**The length of path between two nodes is r...原创 2020-03-08 22:33:54 · 207 阅读 · 0 评论 -
LeetCode 404. Sum of Left Leaves(左叶子之和)
Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. int res = 0; public int sumOfLeftLeaves(TreeNode...原创 2020-03-08 21:44:25 · 196 阅读 · 0 评论 -
LeetCode 572. Subtree of Another Tree(另一个树的子树)
Example 1:Given tree s: 3 / \ 4 5 / \ 1 2Given tree t: 4 / \ 1 2Return true, because t has the same structure and node values with a subtree of s.Example 2:Given tre...原创 2020-03-08 21:24:36 · 183 阅读 · 0 评论 -
LeetCode 617. Merge Two Binary Trees
Example 1:Input: Tree 1 Tree 2 1 2 / \ / \ ...原创 2020-03-08 20:19:07 · 125 阅读 · 0 评论 -
LeetCode 226. Invert Binary Tree(翻转二叉树)
Example:Input: 4 / \ 2 7 / \ / \1 3 6 9Output: 4 / \ 7 2 / \ / \9 6 3 1public TreeNode invertTree(TreeNode root) { if(root == null) retur...原创 2020-03-08 20:12:43 · 155 阅读 · 0 评论 -
LeetCode 543. Diameter of Binary Tree(二叉树直径)
Example:Given a binary tree 1 / \ 2 3 / \ 4 5 Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].Note: The length of path between...原创 2020-03-08 19:58:12 · 144 阅读 · 0 评论 -
LeetCode 110. Balanced Binary Tree
平衡二叉树的条件:左子树是右子树是左右两棵子树高度差不大于1的二叉树public boolean isBalanced(TreeNode root) { //Balanced Binary Tree:which the left and right subtrees of every node differ in height by no more than 1....原创 2020-03-08 19:50:51 · 124 阅读 · 0 评论 -
LeetCode 236+235. Lowest Common Ancestor of a Binary Tree/ Binary Search Tree(二叉树、BST的最近公共祖先)
所有的递归的返回值有4种可能性,null、p、q、公共祖先自顶向下递归public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null) return null; // allow a node to be a descendant...原创 2020-03-07 22:59:44 · 241 阅读 · 1 评论 -
LeetCode 117. Populating Next Right Pointers in Each Node II(填充每个节点的下一个右侧节点指针 II)
与上一题相比:此题的树未要求是完全二叉树要求常量空间,递归的做法实在不懂,所以只能用队列来做public Node connect(Node root) { if(root == null) return null; Queue<Node> queue = new LinkedList<>(); queue.add(r...原创 2020-03-07 22:13:41 · 183 阅读 · 0 评论 -
LeetCode 116. Populating Next Right Pointers in Each Node(填充每个节点的下一个右侧节点指针)
题目描述:给定一个满二叉树(perfect Binary TreeNode),每个节点多了一个next指针,然后将所有的next指针指向它的右边的节点。并且要求空间复杂度是O(1)。法一:BFS,借助队列(但要求空间复杂度为O(1),pass此方法)队列的三种实现形式:在队列中以 (node, level) 的形式存储节点,同时存储其子节点为 (node.left,parent_lev...原创 2020-03-07 21:53:57 · 190 阅读 · 0 评论 -
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(根据前序、中序遍历序列构造二叉树)
HashMap<Integer, Integer> map = new HashMap<>(); public TreeNode buildTree(int[] preorder, int[] inorder) { int len = inorder.length; if(len == 0) return null; ...原创 2020-03-07 21:08:41 · 189 阅读 · 0 评论 -
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历序列构造二叉树)
Note:You may assume that duplicates do not exist in the tree.For example, giveninorder = [9,3,15,20,7]postorder = [9,15,7,20,3]Return the following binary tree: 3 / \ 9 20 / \ ...原创 2020-03-06 19:54:17 · 139 阅读 · 0 评论 -
LeetCode 112+113+437. Path Sum
题目要求:Example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \7 2 1return true, as there exist a root-to-leaf path 5->4->11-&...原创 2020-03-05 17:12:50 · 212 阅读 · 0 评论 -
LeetCode 101. Symmetric Tree(对称二叉树/镜像二叉树)
递归过程:判断两个指针当前节点值是否相等判断 A 的右子树与 B 的左子树是否对称判断 A 的左子树与 B 的右子树是否对称双目运算符的“短路”效应public boolean isSymmetric(TreeNode root) { //自底向上递归 if(root == null) return true; return ...原创 2020-03-05 16:56:07 · 325 阅读 · 0 评论 -
LeetCode 104 + 111. Maximum/Minimum Depth of Binary Tree(二叉树的最大、最小深度)
“自底向上递归”:对于树中的任意一个节点,如果你知道它子节点的答案,你能计算出该节点的答案吗? 如果答案是肯定的,就用自底向上编程public int maxDepth(TreeNode root) { //递归终止条件 if(root == null) return 0; int leftDepth = maxDepth(r...原创 2020-03-05 16:30:25 · 195 阅读 · 0 评论 -
二叉树的遍历(前中后序(递归、迭代、颜色标记法)、层次遍历)
一、前中后序遍历1、递归方式前序public static List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); helper(root, res); return res; }private static voi...原创 2020-03-05 16:10:34 · 855 阅读 · 0 评论