leetcode diagonal traverse 1424

线索二叉树(Morris Traversal)

在前面的文章中总结了二叉树的一些操作,提供了二叉树前中后的递归和非递归的实现。在非递归的实现中,基本思想是利用栈来模拟递归调用遍历的过程,本质上和递归实现没有区别,空间复杂度为\(O(n)\)。是否存在一种算法,它不使用栈也不破坏二叉树结构,但是可以完成对二叉树的遍历?即: 空间复杂度为\(O(1) ......
线索 Traversal Morris

LeetCode每日算法3—无重复字符的最长子串

题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 输入: s = "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 解题思路 这个题目可以使用双指针+map来实现: 首先用双指针维护一个滑动窗口用来剪切子串 开始时, ......
算法 字符 LeetCode

LeetCode222.完全二叉树的节点个数

题目描述 给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。 完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。 示例 提交的代码 你被 ......
节点 个数 LeetCode 222

[Leetcode] 0119. 杨辉三角 II

119. 杨辉三角 II 题目描述 给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。 在「杨辉三角」中,每个数是它左上方和右上方的数的和。 示例 1: 输入: rowIndex = 3 输出: [1,3,3,1] 示例 2: 输入: rowIndex = 0 输出: ......
杨辉三角 Leetcode 0119 II

[Leetcode] 0118. 杨辉三角

118. 杨辉三角 题目描述 给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。 在「杨辉三角」中,每个数是它左上方和右上方的数的和。 示例 1: 输入: numRows = 5 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 示 ......
杨辉三角 Leetcode 0118

[Leetcode] 0112. 路径总和

112. 路径总和 题目描述 给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。 叶子节点 是指没有子节点的节点。 ......
总和 路径 Leetcode 0112

370场leetcode2题题解

100115.找到冠军Ⅰ ​ 一场比赛中共有 n 支队伍,按从 0 到 n - 1 编号。 给你一个下标从 0 开始、大小为 n * n 的二维布尔矩阵 grid 。对于满足 0 <= i, j <= n - 1 且 i != j 的所有 i, j :如果 grid[i][j] == 1,那么 i ......
题解 leetcode2 leetcode 370

[LeetCode] 1359. Count All Valid Pickup and Delivery Options 有效的快递序列数目

Given n orders, each order consists of a pickup and a delivery service. Count all valid pickup/delivery possible sequences such that delivery(i) is al ......
序列 数目 LeetCode Delivery Options

LeetCode111.104二叉树的最大最小深度

题目描述 深度(叶子节点到根节点长度),最大最小深度这里不多赘述。 代码 最大深度 直接上代码: 递归法 import java.util.List; import java.util.ArrayList; import java.util.Deque; import java.util.Linke ......
深度 LeetCode 111.104 111 104

LeetCode/在树上执行操作以后得到的最大分数

有一棵 n 个节点的无向树,节点编号为 0 到 n - 1 ,根节点编号为 0 。给你一个长度为 n - 1 的二维整数数组 edges 表示这棵树,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 有一条边。 同时给你一个长度为 n 下标从 0 开始的整数数组 value ......
分数 LeetCode

LeetCode101.对称二叉树

题目描述 给你一个二叉树的根节点 root , 检查它是否轴对称。 示例 提条的代码 import java.util.List; import java.util.ArrayList; import java.util.Deque; import java.util.LinkedList; imp ......
LeetCode 101

[LeetCode] 1535. Find the Winner of an Array Game

Given an integer array arr of distinct integers and an integer k. A game will be played between the first two elements of the array (i.e. arr[0] and a ......
LeetCode Winner Array 1535 Find

leetcode 第一题 两数之和

题目 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 你可以按任意顺序返回答案。 初级阶段 Java 主要在于 数组定 ......
之和 leetcode

Binary Tree Level Order Traversal

Source Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). Example Given binary tree ......
Traversal Binary Level Order Tree

[LeetCode] 2149. Rearrange Array Elements by Sign

You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers. You should rearrange the ......
Rearrange LeetCode Elements Array 2149

11月LeetCode每日一题: 117. 填充每个节点的下一个右侧节点指针 II

题目描述: 给定一个二叉树: struct Node { int val; Node *left; Node *right; Node *next; } 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL 。 初始状态下,所有 ......
节点 指针 右侧 LeetCode 117

LeetCode102.二叉树的层序遍历

题目描述 给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点) 示例 提交的代码 import java.util.List; import java.util.ArrayList; import java.util.Deque; import java. ......
LeetCode 102

[LeetCode] 2265. Count Nodes Equal to Average of Subtree

Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree. Note: Th ......
LeetCode Average Subtree Count Equal

LeetCode347.前K个高频元素

题目描述 给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。 示例 提交的代码 你被骗了,我没做出来,能想到的方法时间复杂度是nlogn,还不如不写,想到小顶堆了,但是Java这里我不知道怎么实现:( 学习到的东西 经典使用堆实现,但是 ......
LeetCode 元素 347

LeetCode239.滑动窗口最大值

题目描述 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 返回滑动窗口中的最大值 。 示例 提交的代码 import java.util.Deque; import java.util.L ......
最大值 LeetCode 239

[LeetCode] 2574. Left and Right Sum Differences

Given a 0-indexed integer array nums, find a 0-indexed integer array answer where: answer.length == nums.length. answer[i] = |leftSum[i] - rightSum[i] ......
Differences LeetCode Right 2574 Left

LeetCode150.逆波兰表达式求值

题目描述 给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。 请你计算该表达式。返回一个表示表达式值的整数。 示例 提交的代码 import java.util.Deque; import java.util.LinkedList; class Solution { ......
表达式 LeetCode 150

LeetCode1047.删除字符串中的所有相邻重复项

题目描述 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。 在 S 上反复执行重复项删除操作,直到无法继续删除。 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。 示例 提交的代码 import java.util.Deque; import java ......
字符串 字符 LeetCode 1047

[Leetcode] 0111. 二叉树的最小深度

111. 二叉树的最小深度 题目描述 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明:叶子节点是指没有子节点的节点。 示例 1: 输入:root = [3,9,20,null,null,15,7] 输出:2 示例 2: 输入:root = [2,nu ......
深度 Leetcode 0111

[Leetcode] 0110. 平衡二叉树

110. 平衡二叉树 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树。 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。 示例 1: 输入:root = [3,9,20,null,null,15,7] 输出:true 示例 2: 输入:ro ......
Leetcode 0110

LeetCode20.有效的括号

描述 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。 有效字符串需满足: 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 每个右括号都有一个对应的相同类型的左括号。 示例 提交的代码 import java.util.Deque ......
括号 LeetCode 20

[LeetCode] 1274. Number of Ships in a Rectangle

(This problem is an interactive problem.) Each ship is located at an integer point on the sea represented by a cartesian plane, and each integer point ......
Rectangle LeetCode Number Ships 1274

LeetCode225.用队列实现栈

题目描述 请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。 实现 MyStack 类: void push(int x) 将元素 x 压入栈顶。 int pop() 移除并返回栈顶元素。 int top() 返回栈顶元素。 ......
队列 LeetCode 225

LeetCode232.用栈实现队列

题目描述 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): 实现 MyQueue 类: void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元素 int peek() 返回队列开头的元 ......
队列 LeetCode 232

[Leetcode] 0832. 翻转图像

832. 翻转图像 题目描述 给定一个 n x n 的二进制矩阵 image ,先 水平 翻转图像,然后 反转 图像并返回 结果 。 水平翻转图片就是将图片的每一行都进行翻转,即逆序。 例如,水平翻转 [1,1,0] 的结果是 [0,1,1]。 反转图片的意思是图片中的 0 全部被 1 替换, 1  ......
Leetcode 图像 0832