leetcode题解日练--2016.8.6
不给自己任何借口
今日题目:
❤1、二叉树最低公共祖先;
2、逆转链表II
今日摘录:
为着后来的回忆,小心着意地描绘你现在的图画
——冰心《冰心散文集》
236. Lowest Common Ancestor of a Binary Tree | Difficulty: Medium
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 nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______3______/ \
___5__ ___1__/ \ / \
6 _2 0 8/ \
7 4
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
相关题目:Lowest Common Ascestor of a BST
tag:树
题意:找一棵二叉树的两个节点的最低公共祖先。
思路:
1、首先回顾BST情况如何查找LCA?从根节点作为当前判断节点开始,判断当前节点是否同时小于两个待查节点(如果满足说明待LCA在当前节点的右子节点方向),如果同时大于待查找的节点说明LCA在当前节点的左子节点方向,如果均不满足则说明当前节点就是LCA。
但是当时满足了BST这一条件,也就是一个节点的左子节点一定比当前节点小,右子节点一定比当前节点大。但是去掉了这个条件之后我们需要怎么去做呢?还可以用类似的思路,从根节点开始,每次递归去查找我们的待查找节点,每一边第一次遇到任意一个待查找节点就停止继续查找,最后判断根节点的两边是否均有查找到,分情况讨论。
如果左右都找到了,就返回根节点
如果左边找到了而右边没找到,返回左节点,
如果右边找到而左边没找到,返回右节点,
不可能存在左右均没找到的情况
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (!root || root==p || root ==q) return root;TreeNode * left = lowestCommonAncestor(root->left,p,q);TreeNode * right = lowestCommonAncestor(root->right,p,q);if(left && right) return root;else if(left) return left;else return right;}
};
结果:28ms
2、迭代版本,先去找到p和q这两个节点的位置并且记录下来他们的父节点,然后顺着这两条路径去找第一次相关的节点就是LCA。
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {stack<TreeNode*> nodes;unordered_map<TreeNode*,TreeNode*> parent;parent[root] = NULL;nodes.push(root);while(parent.find(p)==parent.end() || parent.find(q)==parent.end()){TreeNode* node = nodes.top();nodes.pop();if(node->left){nodes.push(node->left);parent[node->left] = node;}if(node->right){nodes.push(node->right);parent[node->right] = node;}}set<TreeNode*>path;while(p){path.insert(p);p = parent[p];}while(q && path.find(q)==path.end()){path.insert(q);q = parent[q];}return q;}
};
结果:64ms
92. Reverse Linked List II | Difficulty: Medium
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
相关题目:reverse Linked List
tag:链表
题意:反转链表中的一部分
思路:
1、一样的思路,只不过换成了首先找到逆转的头,然后逆转链表,这里面有一个比较关键的点就是是不是从头还是逆转,如果是那么新头是逆转链表的尾部元素,如果不是新头就是原来的头
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* reverseBetween(ListNode* head, int m, int n) {int count = n-m+1;bool flag;if (m==1) flag = true;else flag = false;if(count==1) return head;ListNode* reverseHead = head;ListNode* before = head;;ListNode * prev = NULL;while(--m>0){before = reverseHead;reverseHead = reverseHead->next;}ListNode* pNode = reverseHead;while(reverseHead && count-->0){ListNode*next = reverseHead ->next;reverseHead ->next = prev;prev = reverseHead;reverseHead = next;}if(!flag){before->next = prev;pNode->next = reverseHead;return head;}else{before->next = reverseHead;return prev;}}
};
结果:4ms
第二次刷解法
首先找到反转之后链表的尾部(即reverseHead),它的前面一个是pre,目的是将pre指向反转链表的头部。当我们要逆转(1,3),其实相当于将2指向1,将3指向2,每次reverseHead指向的节点都是当前节点,取出一个当前节点之后就将reverseHead指向当前cur的下一个节点,做n-m次的反转操作之后节点逆序了,pre也指向了反转之后的头部。
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* reverseBetween(ListNode* head, int m, int n) {if(head==NULL||head->next==NULL||m==n) return head;n-=m;ListNode *preHead = new ListNode(0);preHead->next = head;ListNode*pre = preHead;while(--m) pre = pre->next;ListNode* reverseHead = pre->next;while(n--){ListNode*cur = reverseHead->next;reverseHead->next = cur->next;cur->next = pre->next;pre->next = cur;}return preHead->next;}
};
结果:4ms
leetcode题解日练--2016.8.6相关推荐
- leetcode题解日练--2016.8.26
###不给自己任何借口 今日题目: 1.Ransom Note : tag:字符串 2.在旋转有序数组中查找最小元素II tag:数组|二分 今日摘录: 草在结它的种子,风在摇它的叶子.我们站着, ...
- 900 多道 LeetCode 题解,这个 GitHub 项目值得 Star!
转自 | 码农有道 大家好,我是小 G. 周末风和日丽,适合刷 LeetCode 今天给你们推荐个 GitHub 项目,里面收集了 900 多道 LeetCode 题解,并包含中英文两个版本,适合大多 ...
- PHP版Leetcode题解开始随缘更新
2019独角兽企业重金招聘Python工程师标准>>> PHP版Leetcode题解 我叫skys215,是一名bug工程师. 我接触编程的时间比较早,但是因为我数学不好加上比较懒, ...
- [LeetCode 题解]: Binary Tree Preorder Traversal
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a binary ...
- LeetCode 题解汇总
为什么80%的码农都做不了架构师?>>> LeetCode 题解汇总 转载于:https://my.oschina.net/michao/blog/801863
- leetcode题解【持续更新】
leetcode题解不断更新,以及nowcoder题解.一起加油! 完整请移步我的Github 转载于:https://juejin.im/post/5c8a73d8f265da2de80fa774
- 32位有符号整数_008. 字符串转换整数 (atoi) | Leetcode题解
点击上方"蓝色字体",选择"设为星标" 每天复习一道面试题,轻松拿大厂Offer~ 题目描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先 ...
- leetcode题解132-分割回文串 II
问题描述 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文. 返回符合要求的 最少分割次数 . 示例 1: 输入:s = "aab" 输出:1 解释:只需一次分割 ...
- leetcode题解131-分割回文串
问题描述 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 .返回 s 所有可能的分割方案. 回文串 是正着读和反着读都一样的字符串. 示例 1: 输入:s = "aa ...
- [LeetCode 题解]: Roman to Interger
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a roman ...
最新文章
- Firefox下载自动保存
- 机器学习知识点(三十四)机器学习类学习资源
- python无法启动该程序因为计算机中丢失_python报错:无法启动此程序,因为计算机中丢失...
- c++Cocktail Selection Sort鸡尾酒选择排序的实现算法(附完整源码)
- linux的mysql小记
- pyecharts本地文件_python数据可视化-pyecharts插件使用文档
- excel文件存入mysql_解析excel文件并将数据导入到数据库中
- Copy as Markdown - 将页面链接按照 Markdown 格式copy
- 已知前序(先序)与中序输出后序
- mysql sql数据排名_查询数据排名情况SQL
- 阿里云 linux centos nginx安装IP ssl https证书
- java之接口适配器
- Sqlmap安装教程
- 安装element ui
- SketchUp 导出 fbx文件 单位 错误
- 文件mime类型大全
- Pandas sample随机抽样
- 一元三次方程c语言程序,一元三次方程求解
- 在线hash密码破解网站列表
- 全面概述什么是人工智能?
热门文章
- C++17 实现日期和时间相关编程
- 在线商城和商城后台管理系统
- MVC.MVVM.MVP的理解
- 欢迎关注异贝!今天与您一起分享服装行业异贝引客方案设计!
- 从全面了解高级计量经济学体系+stata实操+实证研究前沿+论文+大数据机器学习+空间计量......._Stata高级计量经济学 陈强
- 《拥抱机器人时代——Servo杂志中文精华合集》——4.3 理解智能设备
- OFDM学习笔记(十)(MIMO-OFDM技术)
- NetSuite 二次开发工具介绍
- java截取图片截图画框
- 手把手教你搭建免费云平台——新浪云