Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.Input: [1,2,3,4,5]1/2 3/4 5Output: return the root of the binary tree [4,5,2,#,#,3,1]4/5 2/3 1 Confused what [4,5,2,#,#,3,1] mea...
【030-Substring with Concatenation of All Words(串联全部单词的子串)】【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】原题 You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, given: s: "barfoot...
目录# 前端与算法 leetcode 283. 移动零题目描述概要提示解析解法一:暴力法解法二:双指针法算法传入[0,1,0,3,12]的运行结果执行结果GitHub仓库# 前端与算法 leetcode 283. 移动零题目描述给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。示例:输入: [0,1,0,3,12] 输出: [1,3,12,0,0]说明:必须在原数组上操作,不能拷贝额外的数组。尽量减少操作次数。283. 移动零概要这个问题属于 “数组变...
题目:Given a binary tree, return the level order traversal of its nodes‘ values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3/ 9 20/ 15 7 return its level order traversal as:[[3],[9,20],[15,7] ]分析:给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。可以立刻想到两种方法,也就是BFS和DFS。先说BFS,BFS实际...
算法题:两数之和。给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。示例:给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] import java.util.HashMap;public class TwoSum {public static void main(String[] args) {int[] nums = {3, 3, 3};...
题目描述:给一个二叉树,返回该二叉树的最大深度Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Note: A leaf is a node with no children.Example:Given binary tree [3,9,20,null,null,15,7], 3/ 9 20/ 15 7return its depth = 3.思路:采用递归,从根节点出发,获得根节点的左子树和右子树深度...
【LeetCode】Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.递归和非递归,此提比较简单。广度优先遍历即可。关键之处就在于如何保持访问深度。下面是4种代码: 1import java.util.ArrayList;2import java.util.LinkedList;3import java.util.List;4import java.util.Queue;5 ...
题目:给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [1,2,2,3,4,4,3] 是对称的。1/22/ \ /3443 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:1/22\ 33链接:https://leetcode-cn.com/problems/symmetric-tree最开始的思路是用层次遍历,每一行看看是不是回文数组,但后来发现例如样例2那样的树就会判断错误,递归也没想好怎么个调用思路。 正确的递归思路:来源:https://leetcode-cn.com/u/haventmetyo...
【题目】There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it‘s horizontal, y-coordinates don‘t matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.An arrow can be shot up exactly v...
其实算法并没有太难,无论是3次遍历还是1次遍历,都不会超时,我在做的时候,如何使用测试数据反而困扰了我很久。读取测试数据将测试数据放在文件中读取,这里将文件指针改为stdin应该也是一样的效果。#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <string.h>int main() {int boardSize = 9;int boardColSize = 9;char** board = (char **) malloc(sizeof(char *) * boardSize);// 打开文件FILE *fp = f...
这是悦乐书的第285次更新,第302篇原创01 看题和准备今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671)。给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树中的每个节点具有恰好两个或零个子节点。 如果节点具有两个子节点,则该节点的值是其两个子节点中的较小值。给定这样的二叉树,您需要输出由整个树中所有节点的值组成的集合中的第二个最小值。如果不存在这样的第二个最小值,则输出-1。例如: 2/...
数组:加一题干:给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。参考样例:示例?1:输入: [1,2,3]输出: [1,2,4]解释: 输入数组表示数字 123。示例?2:输入: [4,3,2,1]输出: [4,3,2,2]解释: 输入数组表示数字 4321。这道题是一道数组的基础题,其本质是一道模拟计算题。这道题有一定的工程应用意...
题目描述:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4]今日学习: 1.复习树结构 2.复习树的遍历 3.学习使用nginx代理题解://从叶子节点开始向上查找,保证找到的公共祖先...
我的LeetCode:https://leetcode-cn.com/u/ituring/我的LeetCode刷题源码[GitHub]:https://github.com/izhoujie/AlgorithmciiLeetCode 面试题32 - II. 从上到下打印二叉树 II题目从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。例如:给定二叉树:?[3,9,20,null,null,15,7],3/ 9 20/ 15 7 返回其层次遍历结果:[[3],[9,20],[15,7] ] 提示:节点总数 <= 1000注意:本题与主站 102 题相同:h...
你有一棵二叉树,这棵二叉树有个小问题,其中有且只有一个无效节点,它的右子节点错误地指向了与其在同一层且在其右侧的一个其他节点。给定一棵这样的问题二叉树的根节点 root ,将该无效节点及其所有子节点移除(除被错误指向的节点外),然后返回新二叉树的根结点。自定义测试用例:测试用例的输入由三行组成:TreeNode root int fromNode (在 correctBinaryTree 中不可见) int toNode (在 correctBinaryTree 中不可见) 当以...