leetcode
leetcode刷题
中华小当家yo
自学习
展开
-
leetcode236. Lowest Common Ancestor of a Binary Tree
题目描述Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two node...原创 2020-03-30 16:58:17 · 142 阅读 · 0 评论 -
leetcode230. Kth Smallest Element in a BST
题目描述Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Example 1:Input: root = [3,...原创 2020-03-28 16:19:56 · 153 阅读 · 0 评论 -
leetcode108. Convert Sorted Array to Binary Search Tree
题目描述Given an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of...原创 2020-03-28 16:06:44 · 247 阅读 · 0 评论 -
leetcode98. Validate Binary Search Tree
题目描述Given a binary tree, determine if it is a valid 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.Th...原创 2020-03-27 16:29:10 · 130 阅读 · 0 评论 -
leetcode235. Lowest Common Ancestor of a Binary Search Tree
题目描述Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw...原创 2020-03-26 20:09:25 · 158 阅读 · 0 评论 -
leetcode437. Path Sum III(递归嵌套递归)
题目描述You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it ...原创 2020-03-25 16:56:13 · 111 阅读 · 0 评论 -
leetcode404. Sum of Left Leaves
题目描述Find the sum of all left leaves in a given binary tree.Example:3/ 9 20/ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.来源:力扣(LeetCode)...原创 2020-03-22 16:59:41 · 124 阅读 · 0 评论 -
leetcode112. Path Sum
题目描述Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Note: A leaf is a node with no children.Examp...原创 2020-03-21 16:42:48 · 84 阅读 · 0 评论 -
leetcode222. Count Complete Tree Nodes(只掌握了普适方法)
题目描述Given a complete binary tree, count the number of nodes.Note:Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely ...原创 2020-03-19 17:11:42 · 97 阅读 · 0 评论 -
leetcode101. Symmetric Tree
题目描述Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric:1/ 2 2/ \ / 3 4 4 3But the fol...原创 2020-03-19 16:43:39 · 146 阅读 · 0 评论 -
leetcode100. Same Tree
题目描述Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Examp...原创 2020-03-19 16:31:01 · 111 阅读 · 0 评论 -
leetcode226. Invert Binary Tree
题目描述Invert a binary tree.Example:Input: 4/ 2 7/ \ / 1 3 6 9Output: 4/ 7 2/ \ / 9 6 3 1代码递归#include <iostream>using namespace std;/// Definition fo...原创 2020-03-18 17:31:00 · 128 阅读 · 0 评论 -
leetcode104. 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 the farthest leaf node.Note: A leaf is a node with no childr...原创 2020-03-16 17:02:25 · 182 阅读 · 0 评论 -
优先队列
#include <iostream>#include <queue>#include <ctime>using namespace std;bool myCmp(int a , int b){ if(a%10 != b%10) //比较个位数 return a%10 > b%10; return a >...原创 2020-03-12 16:10:34 · 90 阅读 · 0 评论 -
leetcode279. Perfect Squares
题目描述Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.Example 1:Input: n = 12Output: 3Explanation: 12 = 4 + 4 + 4.来源:力扣(Lee...原创 2020-03-11 17:42:03 · 116 阅读 · 0 评论 -
leetcode199. Binary Tree Right Side View
题目描述Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.Example:Input: [1,2,3,null,5,null,4]Output: [1, 3,...原创 2020-03-10 17:14:37 · 119 阅读 · 0 评论 -
leetcode103. Binary Tree Zigzag Level Order Traversal
题目描述Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary...原创 2020-03-10 16:59:17 · 88 阅读 · 0 评论 -
leetcode107. Binary Tree Level Order Traversal II(同102)
题目描述Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,null,...原创 2020-03-09 17:01:08 · 104 阅读 · 0 评论 -
leetcode102. Binary Tree Level Order Traversal
题目描述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...原创 2020-03-09 16:19:50 · 121 阅读 · 0 评论 -
leetcode341. Flatten Nested List Iterator(浅)
题目描述Given a nested list of integers, implement an iterator to flatten it.Each element is either an integer, or a list – whose elements may also be integers or other lists.Example 1:Input: [[1,1],2...原创 2020-03-09 15:41:13 · 77 阅读 · 0 评论 -
leetcode145. Binary Tree Postorder Traversal
题目描述Given a binary tree, return the postorder traversal of its nodes’ values.Example:Input: [1,null,2,3]12/3Output: [3,2,1]来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/binary-tree-pos...原创 2020-03-06 21:22:35 · 91 阅读 · 0 评论 -
leetcode94. Binary Tree Inorder Traversal(同144)
题目描述Given a binary tree, return the inorder traversal of its nodes’ values.Example:Input: [1,null,2,3]12/3Output: [1,3,2]来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/binary-tree-inord...原创 2020-03-06 21:08:42 · 121 阅读 · 0 评论 -
leetcode144. Binary Tree Preorder Traversal(递归、非递归)
题目描述Given a binary tree, return the preorder traversal of its nodes’ values.Example:Input: [1,null,2,3]12/3Output: [1,2,3]来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/binary-tree-preo...原创 2020-03-06 20:56:13 · 171 阅读 · 0 评论 -
leetcode150. Evaluate Reverse Polish Notation
题目描述Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Note:Division between two integer...原创 2020-03-04 16:52:16 · 261 阅读 · 0 评论 -
leetcode20. Valid Parentheses
题目描述Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type o...原创 2020-03-04 16:26:16 · 174 阅读 · 0 评论 -
leetcode234. Palindrome Linked List(类似143)
题目描述Given a singly linked list, determine if it is a palindrome.Example 1:Input: 1->2Output: false来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/palindrome-linked-list著作权归领扣网络所有。商业转载请联系官...原创 2020-03-03 17:48:49 · 79 阅读 · 0 评论 -
leetcode143. Reorder List
题目描述Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You may not modify the values in the list’s nodes, only nodes itself may be changed.Example 1:Given 1->2...原创 2020-03-02 17:15:36 · 146 阅读 · 0 评论 -
leetcode61. Rotate List
题目描述Given a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExpl...原创 2020-02-29 19:45:01 · 175 阅读 · 0 评论 -
leetcode19. Remove Nth Node From End of List
题目描述Given a linked list, remove the n-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, ...原创 2020-02-29 17:30:40 · 103 阅读 · 0 评论 -
leetcode237. Delete Node in a Linked List
题目描述Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Given linked list – head = [4,5,1,9], which looks like following:Example 1:Input: he...原创 2020-02-28 16:49:38 · 97 阅读 · 0 评论 -
leetcode148. Sort List(浅)
题目描述Sort a linked list in O(n log n) time using constant space complexity.Example 1:Input: 4->2->1->3Output: 1->2->3->4来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/sort-l...原创 2020-02-28 16:36:03 · 70 阅读 · 0 评论 -
leetcode147. Insertion Sort List(浅)
题目描述Sort a linked list using insertion sort.A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.With each iteration one elem...原创 2020-02-27 20:13:59 · 71 阅读 · 0 评论 -
leetcode25. Reverse Nodes in k-Group(理解尚浅)
题目描述Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number...原创 2020-02-26 22:30:38 · 207 阅读 · 0 评论 -
leetcode24. Swap Nodes in Pairs
题目描述Given a linked list, swap every two adjacent nodes and return its head.You may not modify the values in the list’s nodes, only nodes itself may be changed.Example:Given 1->2->3->4, yo...原创 2020-02-25 15:53:46 · 81 阅读 · 0 评论 -
leetcode21. Merge Two Sorted Lists
题目描述Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output: ...原创 2020-02-25 14:11:26 · 79 阅读 · 0 评论 -
leetcode82. Remove Duplicates from Sorted List II
题目描述Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.Example 1:Input: 1->2->3->3->4->4->5Output: 1-&...原创 2020-02-24 19:55:39 · 135 阅读 · 0 评论 -
leetcode203. Remove Linked List Elements
题目描述Remove all elements from a linked list of integers that have value val.Example:Input: 1->2->6->3->4->5->6, val = 6Output: 1->2->3->4->5来源:力扣(LeetCode)链接:https:...原创 2020-02-24 16:57:24 · 173 阅读 · 0 评论 -
leetcode445. Add Two Numbers II
题目描述You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and ret...原创 2020-02-23 17:01:48 · 104 阅读 · 0 评论 -
leetcode2. Add Two Numbers
题目描述You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and ret...原创 2020-02-23 16:06:29 · 92 阅读 · 0 评论 -
leetcode328. Odd Even Linked List
题目描述Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it ...原创 2020-02-22 16:53:56 · 106 阅读 · 0 评论