![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
宋霖轩
已搬迁cnblog
展开
-
Binary Tree Pruning——LeetCode
大致题目要求: 本质上就是剪枝问题,意图将每一个树上的0子树删除(节点也是子树),但是注意,父节点为0但是子节点为1的节点必须要进行保留。 其实本质上就是一个遍历问题,后序遍历可以很好的满足这个需求,即先访问子节点再访问父节点,并且根据判断来进行节点删除。 在节点删除上,往往会陷入一个泥潭,就是过度的去追求父节点的寻找,从而来利用父节点的指针来删除子节点。利用递归时,可以利用返回...原创 2018-05-22 15:57:12 · 134 阅读 · 0 评论 -
LeetCode 86. Partition List
86.Partition ListMedium578158FavoriteShareGiven a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the ...原创 2019-03-18 23:48:03 · 119 阅读 · 0 评论 -
LeetCode 969. Pancake Sorting
Given an arrayA, we can perform apancake flip:We choose some positive integerk<= A.length, then reverse the order of the firstkelements ofA. We want to perform zero or more pancake flips (...原创 2019-03-20 21:17:55 · 213 阅读 · 0 评论 -
LeetCode 88. Merge Sorted Array
Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.Note:The number of elements initialized innums1andnums2aremandnrespectively. You may assume tha...原创 2019-03-20 20:14:20 · 148 阅读 · 0 评论 -
LeetCode 26. Remove Duplicates from Sorted Array
Given a sorted arraynums, remove the duplicatesin-placesuch that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this bymodifyi...原创 2019-03-20 19:27:29 · 147 阅读 · 0 评论 -
LeetCode 27. Remove Element
Given an arraynumsand a valueval, remove all instances of that valuein-placeand return the new length.Do not allocate extra space for another array, you must do this bymodifying the input arra...原创 2019-03-20 19:02:06 · 101 阅读 · 0 评论 -
LeetCode 09 Remove Nth Node From End of List
Given a linked list, remove then-th node from the end of list and return its head.Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, t...原创 2019-03-11 18:04:04 · 125 阅读 · 0 评论 -
LeetCode 950 Reveal Cards In Increasing Order 排序的反向推导
In a deck of cards, every card has a unique integer. You can order the deck inany order you want.Initially, all the cards start face down (unrevealed) in one deck.Now, you do the following steps...原创 2019-03-08 16:37:01 · 133 阅读 · 0 评论 -
LeetCode 832 Flipping an Image 水题
Given a binary matrixA, we want to flip the image horizontally, then invert it, and return the resulting image.To flip an image horizontally means that each row of the image is reversed. For examp...原创 2019-03-08 15:57:54 · 196 阅读 · 0 评论 -
LeetCode 905 Sort Array By Parity 水题
Given an arrayAof non-negative integers, return an array consisting of all the even elements ofA, followed by all the odd elements ofA.You may return any answer array that satisfies this conditi...原创 2019-03-08 15:48:23 · 135 阅读 · 0 评论 -
Leetcode 977 Squares of a Sorted Array 水题
Given an array of integersAsorted in non-decreasing order,return an array of the squares of each number,also in sorted non-decreasing order.Example 1:Input: [-4,-1,0,3,10]Output: [0,1,9,1...原创 2019-03-08 15:38:52 · 179 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
一个老生常谈的问题;通过对后序序列进行分析就可以知道,最后一个节点作为根节点,倒数第二个子序列作右子树,最浅的一个序列作左子树,通过对后序序列寻找根节点在进行子树划分,并且通过递归再次寻找;/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left...原创 2018-05-27 19:24:49 · 139 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal
经典问题,通过先序序列和中序序列来构造一个二叉树; 要抓住的点就是先序序列中,首元素就是需要构造树的根元素。所以采用递归的方法,判断左子树和右子树序列,然后再次递归再次判定。这次题目遇到了Runtime的问题,总的来说是只需要从preorder中找第一个节点做根节点,从inorder序列中找左右子树就可以。/** * Definition for a binary tree nod...原创 2018-05-27 19:13:43 · 168 阅读 · 0 评论 -
Find Duplicate Subtree——Leetcode
题目大意: 给定一个树,来寻找其相同的子树,本质上是一个遍历寻找问题。 随着做一些关于树的题目,发现树的问题离不开遍历方法。一般的遍历方法也就四种,前序、中序、后序,以及层遍历。 该题目有两种解法,其一是及进行前序遍历,进行相同子树的相同父节点判别,即如果寻找到相同的父节点,自顶向下判别子树是否相同。但是自己实验这种方法后发现时间复杂度过高。当然也可以进行树的数组化,变成一个一维...原创 2018-05-22 16:11:17 · 180 阅读 · 0 评论 -
LeetCode 61. Rotate List
Given a linkedlist, rotate the list to the right bykplaces, wherekis non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplan...原创 2019-03-19 00:05:03 · 107 阅读 · 0 评论