![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode题解
文章平均质量分 59
seu_nuaa_zc
这个作者很懒,什么都没留下…
展开
-
5.1.4—二叉树的遍历—Binary Tree Level Order Traversal
描述Given a binary tree, return the level order traversal of its nodes’ values. (ie, from le to right, level bylevel).For example: Given binary tree {3,9,20,#,#,15,7},3/ \9 20/ \15 7r原创 2017-08-07 21:29:44 · 169 阅读 · 0 评论 -
5.1.5—二叉树的遍历—Binary Tree Level Order Traversal II
描述Given a binary tree, return the boom-up level order traversal of its nodes’ values. (ie, from le toright, level by level from leaf to root).For example: Given binary tree {3,9,20,#,#,15,7},原创 2017-08-07 21:30:08 · 203 阅读 · 0 评论 -
5.1.6—二叉树的遍历—Binary Tree Zigzag Level Order Traversal
描述Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from le to right,then right to le for the next level and alternate between).For example: Given binary原创 2017-08-07 21:30:34 · 247 阅读 · 0 评论 -
5.1.8—二叉树的遍历—Same Tree
描述Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the samevalue.#includ原创 2017-08-07 21:31:55 · 209 阅读 · 0 评论 -
5.1.9—二叉树的遍历—Symmetric Tree
描述Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the samevalue.#includ原创 2017-08-07 21:32:30 · 192 阅读 · 0 评论 -
5.1.10—二叉树的遍历—Balanced Binary Tree
描述Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of thetwo subtrees of every node never di原创 2017-08-07 21:33:11 · 211 阅读 · 0 评论 -
5.1.11—二叉树的遍历—Flatten Binary Tree to Linked List
描述Given a binary tree, flaen it to a linked list in-place.For example, Given 1 / \ 2 5/ \ \3 4 6e flaened tree should look like:1\2\3\4\5\6#include "B原创 2017-08-07 21:34:01 · 169 阅读 · 0 评论 -
5.2.1—二叉树的构建—Construct Binary Tree from Preorder and Inorder Traversal
描述Given preorder and inorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.#include "BinaryTree.h"#includeusing namespace std;原创 2017-08-07 21:35:29 · 218 阅读 · 0 评论 -
2.1.1—线性表—Remove Duplicates from Sorted Array
描述Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in place wit原创 2017-08-01 12:41:55 · 186 阅读 · 0 评论 -
2.1.2—线性表—Remove Duplicates from Sorted Array II
描述Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [原创 2017-08-01 12:51:31 · 154 阅读 · 0 评论 -
2.1.3—线性表—Search in Rotated Sorted Array
描述Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return原创 2017-08-01 22:28:31 · 172 阅读 · 0 评论 -
2.1.4—线性表—Search in Rotated Sorted Array II
描述Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in th原创 2017-08-01 22:37:17 · 147 阅读 · 0 评论 -
2.1.6 —线性表—Longest Consecutive Sequence
描述Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example, Given[100, 4, 200, 1, 3, 2], e longest consecutive elements sequence is[1,2,原创 2017-08-01 23:45:55 · 265 阅读 · 0 评论 -
2.1.9 —线性表—3Sum Closest
描述Given an array S of n integers, find three integers inS such that the sum is closest to a given number,target. Return the sum of the three integers. You may assume that each input would ha原创 2017-08-02 09:12:38 · 137 阅读 · 0 评论 -
2.1.10 —线性表—4Sum
描述Given an array S of n integers, are there elementsa, b, c, andd inS such thata+b+c+d= target?Find all unique quadruplets in the array which gives the sum of target.Note:• Ele原创 2017-08-02 09:19:22 · 147 阅读 · 0 评论 -
2.1.11 —线性表—Remove Element
描述Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn’t matter what you leave beyond the new length.原创 2017-08-02 09:28:20 · 388 阅读 · 0 评论 -
2.1.13 —线性表—Permutation Sequence
描述The set [1,2,3,…,n]contains a total of n! unique permutations.By listing and labeling all of the permutations in order, We get the following sequence (ie, forn = 3):"123""132""21原创 2017-08-02 09:56:20 · 146 阅读 · 0 评论 -
2.1.14 —线性表—Valid Sudoku
描述Determine if a Sudoku is valid, according to: Sudoku Puzzles - e Ruleshttp://sudoku.com.au/TheRules.aspx.The Sudoku board could be partially filled, where empty cells are filled with the原创 2017-08-02 10:10:23 · 143 阅读 · 0 评论 -
5.2.2—二叉树的构建—Construct Binary Tree from Inorder and Postorder Traversal
描述Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.#include "BinaryTree.h"#includeusing namespace原创 2017-08-08 23:41:07 · 163 阅读 · 0 评论 -
5.3.3—二叉查找树—Validate Binary Sear Tree
描述Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:#include "BinaryTree.h"#include#include#includeusing namespace std;vector pat原创 2017-08-08 23:41:28 · 177 阅读 · 0 评论 -
5.3.4—二叉查找树—Convert Sorted Array to Binary Sear Tree
描述Given an array where elements are sorted in ascending order, convert it to a height balanced BST.#include "BinaryTree.h"#include#include#includeusing namespace std;vector path;//===由有序数组原创 2017-08-08 23:41:42 · 140 阅读 · 0 评论 -
5.3.5—二叉查找树—Convert Sorted List to Binary Sear Tree
描述Given a singly linked list where elements are sorted in ascending order, convert it to a height balancedBST.#pragma once#includestruct BinaryTreeNode{ int m_nValue; B原创 2017-08-08 23:41:55 · 154 阅读 · 0 评论 -
5.4.1—二叉树的递归—Minimum Depth of Binary Tree
描述Given 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 thenearest leaf node.#include "BinaryTree.h"#includ原创 2017-08-08 23:42:11 · 140 阅读 · 0 评论 -
5.4.2—二叉树的递归—Maximum Depth of Binary Tree
描述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 thefarthest leaf node.#include "BinaryTree.h"#includ原创 2017-08-08 23:43:06 · 155 阅读 · 0 评论 -
5.4.3—二叉树的递归—Path Sum
描述Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all thevalues along the path equals the given sum.For example: Given the below binary tree and原创 2017-08-08 23:43:20 · 168 阅读 · 0 评论 -
5.4.4—二叉树的递归—Path Sum II
描述Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.For example: Given the below binary tree and sum = 22,5/ \4 8/ / \11 13 4/ \ / \原创 2017-08-08 23:43:33 · 167 阅读 · 0 评论 -
5.4.7—二叉树的递归—Sum Root to Leaf Numbers
描述Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total原创 2017-08-08 23:43:56 · 159 阅读 · 0 评论 -
6.1—排序—Merge Sorted Array
描述Given two sorted integer arrays A and B, merge B into A as one sorted array.Note: You may assume that A has enough space to hold additional elements from B. e number ofelements initialized in原创 2017-08-08 23:44:06 · 125 阅读 · 0 评论 -
2.1.16 —线性表—Rotate Image
描述You are given an n × n2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up: Could you do this in-place?#include#includeusing namespace std;const in原创 2017-08-02 10:43:57 · 158 阅读 · 0 评论 -
6.2—排序—Merge Two Sorted Lists
描述Merge two sorted linked lists and return it as a new list. e new list should be made by splicingtogether the nodes of the first two lists.#includeusing namespace std;struct node{ int da原创 2017-08-09 10:16:09 · 125 阅读 · 0 评论 -
6.3—排序—Merge k Sorted Lists
描述Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.#include#include;using namespace std;struct node{ int data; node *next;};class myl原创 2017-08-09 10:16:30 · 149 阅读 · 0 评论 -
6.4—排序—Insertion Sort List
描述Sort a linked list using insertion sort.#include#include;using namespace std;struct node{ int data; node *next;};class mylist{public: node *head;public: mylist() { head = new n原创 2017-08-09 10:16:40 · 142 阅读 · 0 评论 -
6.5—排序—Sort List
描述Sort a linked list in O(nlogn) time using constant space complexity.#include#include;using namespace std;struct node{ int data; node *next;};node *CreateNode(int a[], int n){ if (n <原创 2017-08-09 10:16:53 · 165 阅读 · 0 评论 -
6.6—排序—First Missing Positive
描述Given an unsorted integer array, find the first missing positive integer.For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant s原创 2017-08-09 10:17:04 · 194 阅读 · 0 评论 -
6.7—排序—Sort Colors
描述Given an array with n objects colored red, white or blue, sort them so that objects of the same colorare adjacent, with the colors in the order red, white and blue.Here, we will use the intege原创 2017-08-09 10:17:14 · 165 阅读 · 0 评论 -
7.1—查找—Sear for a Range
描述Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the order of O(log n).If the target is not found原创 2017-08-09 10:17:36 · 192 阅读 · 0 评论 -
7.2—查找—Sear Insert Position
描述Given a sorted array and a target value, return the index if the target is found. If not, return the indexwhere it would be if it were inserted in order.You may assume no duplicates in the arr原创 2017-08-09 10:18:01 · 180 阅读 · 0 评论 -
7.3—查找—Sear a 2D Matrix
描述Write an efficient algorithm that searches for a value in an m×n matrix. is matrix has the followingproperties:Integers in each row are sorted from le to right.The first integer of each ro原创 2017-08-09 10:18:12 · 148 阅读 · 0 评论 -
8.1—暴力枚举法—Subsets
描述Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,原创 2017-08-09 10:18:28 · 222 阅读 · 0 评论 -
5.1.7—二叉树的遍历—Recover Binary Sear Tree
描述Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note: A solution using O(n) space is prey straight forward. Could you devise原创 2017-08-07 21:31:23 · 238 阅读 · 0 评论