![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构
文章平均质量分 74
zazapan
这个作者很懒,什么都没留下…
展开
-
删除链表中的元素
ListNode *removeElements(ListNode *head, int val) { while ( head != NULL && head->val == val){ head = head->next; } if ( head == NULL) { re原创 2017-03-27 21:27:06 · 182 阅读 · 0 评论 -
克隆二叉树
深度复制一个二叉树。给定一个二叉树,返回一个他的 克隆品 。class Solution { public: /** * @param root: The root of binary tree * @return root of new tree */ void look (TreeN原创 2017-04-23 21:59:44 · 163 阅读 · 0 评论 -
把排序数组转换为高度最小的二叉搜索树
给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树样例给出数组 [1,2,3,4,5,6,7], 返回 4 / \ 2 6 / \ / \1 3 5 7class Solution { public: /** * @param A: A sorted (increa原创 2017-04-23 22:06:35 · 142 阅读 · 0 评论 -
将二叉树拆成链表
样例 1 \ 1 2 / \ \ 2 5 => 3 / \ \ \ 3 4 6 4 \ 5原创 2017-04-23 22:21:13 · 125 阅读 · 0 评论 -
子树
样例下面的例子中 T2 是 T1 的子树: 1 3 / \ / T1 = 2 3 T2 = 4 / 4下面的例子中 T2 不是 T1 的子树: 1 3 / \原创 2017-04-23 22:26:51 · 131 阅读 · 0 评论 -
翻转二叉树
样例 1 1 / \ / \2 3 => 3 2 / \ 4 4class Solution { public: /** * @param root: a TreeNode, the root of the binary tree * @re原创 2017-04-23 22:28:07 · 150 阅读 · 0 评论 -
将二叉树拆成链表
样例 1 \ 1 2 / \ \ 2 5 => 3 / \ \ \ 3 4 6 4 \ 5原创 2017-04-23 22:32:14 · 116 阅读 · 0 评论 -
等价二叉树
样例 1 1 / \ / \ 2 2 and 2 2 / /4 4就是两棵等价的二叉树。 1 1 / \ / \ 2 3 and 2 3 /原创 2017-04-23 22:33:28 · 103 阅读 · 0 评论 -
二叉树的层次遍历 - C++
样例给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \9 20 / \ 15 7返回他的分层遍历结果:[ [3], [9,20], [15,7]]class Solution { /** * @param root : The root of binary tree.原创 2017-04-23 22:34:44 · 539 阅读 · 0 评论 -
在二叉查找树中插入节点
样例给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 2 2 / \ / \1 4 --> 1 4 / / \ 3 3 6解题思路:首先理解二叉查找树的定义,插入的过程中只需三点:1,是否二叉树为空,如果为空插入节点为根节原创 2017-05-21 23:39:05 · 185 阅读 · 0 评论 -
两数之和
要求:给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 1 到 n,不是以 0 开头。实现代码:class Solution {public: /* * @param numbers : An array of Intege原创 2017-06-11 21:30:29 · 173 阅读 · 0 评论 -
二叉树的最大深度
给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的距离。样例给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5这个二叉树的最大深度为3.class Solution { public: /** * @param root: The root of binary原创 2017-04-23 21:41:29 · 141 阅读 · 0 评论 -
二叉树的最大节点
在二叉树中寻找值最大的节点并返回。样例给出如下一棵二叉树: 1 / \ -5 2 / \ / \0 3 -4 -5 返回值为 3 的节点。class Solution { public: /** * @param root the root of binary tree原创 2017-04-23 21:39:09 · 276 阅读 · 0 评论 -
链表倒数第n个节点
给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1class Solution {public: /** * @param head: The first node of linked list. * @param n: An integer. * @return: Nth to last node of a si原创 2017-03-31 13:38:37 · 149 阅读 · 0 评论 -
在O(1)时间复杂度删除链表节点
给定 1->2->3->4,和节点 3,删除 3 之后,链表应该变为 1->2->4class Solution {public: /** * @param node: a node in the list should be deleted * @return: nothing */ void deleteNod原创 2017-03-31 19:58:50 · 168 阅读 · 0 评论 -
两两交换链表中的节点
给出 1->2->3->4, 你应该返回的链表是 2->1->4->3ListNode* swapPairs(ListNode* head) { // Write your code here ListNode *x; x=head; if ( head == NULL) { r原创 2017-03-31 20:07:23 · 159 阅读 · 0 评论 -
删除链表中倒数第n个节点
给出链表1->2->3->4->5->null和 n = 2.删除倒数第二个节点之后,这个链表将变成1->2->3->5->null.istNode *removeNthFromEnd(ListNode *head, int n) { // write your code here ListNode *a, *b, *pre;原创 2017-03-31 20:35:30 · 220 阅读 · 0 评论 -
删除排序链表中的重复元素
给出 1->1->2->null,返回 1->2->null给出 1->1->2->3->3->null,返回 1->2->3->nullclass Solution {public: /** * @param head: The first node of linked list. * @return: head no原创 2017-03-31 20:42:03 · 164 阅读 · 0 评论 -
链表求和
/**给出两个链表 3->1->5->null 和 5->9->2->null,返回 8->0->8->null最后这四个题实在不会做了,抄的别人的 * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *转载 2017-03-31 22:14:56 · 228 阅读 · 0 评论 -
链表插入排序
Given 1->3->2->0->null, return 0->1->2->3->null借鉴别人的,/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { *转载 2017-03-31 22:17:21 · 111 阅读 · 0 评论 -
二叉树的中序遍历
给出一棵二叉树,返回其中序遍历给出二叉树 {1,#,2,3}, 1 \ 2 / 3返回 [1,3,2].public: vector a; void look(TreeNode *root) { if(root==NULL) return;原创 2017-04-23 21:22:22 · 101 阅读 · 0 评论 -
二叉树的前序遍历
给出一棵二叉树,返回其节点值的前序遍历。样例给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3].class Solution {public: /** * @param root: The root of binary tree. * @return: Pr原创 2017-04-23 21:26:44 · 115 阅读 · 0 评论 -
二叉树的后序遍历
给出一棵二叉树,返回其节点值的后序遍历。给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3返回 [3,2,1]class Solution { /** * @param root: The root of binary tree. * @return: Postord原创 2017-04-23 21:28:56 · 145 阅读 · 0 评论 -
中位数
题目:给定一个未排序的整数数组,找到其中位数。中位数是排序后数组的中间值,如果数组的个数是偶数个,则返回排序后数组的第N/2个数。解题代码:class Solution {public: /** * @param nums: A list of integers. * @return: An integer denotes the middle num原创 2017-06-11 22:18:39 · 257 阅读 · 0 评论