leetcode学习笔记4
2016-02-02 15:20
225 查看
Symmetric Tree
Given a binarytree, check whether it is a mirror of itself (ie, symmetric around its center).
For example,this binary tree is symmetric:
1
/\
2 2
/ \/ \
3 44 3
But thefollowing is not:
1
/\
2 2
\ \
3 3
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
return help(root->left,root->right);
}
private:
bool help(TreeNode* left,TreeNode* right){
if(left==NULL&&right==NULL) return true;
if(left==NULL||right==NULL) return false;
if(left->val!=right->val) return false;
return(help(left->right,right->left)&&help(left->left,right->right));
}
};
注:重点!!要注意返回值,if(left->val==right->val)return true; else return false;是错误的!!易犯错误!!
Balanced Binary Tree
Given a binary tree,determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as abinary tree in which the depth of the two subtrees of every node never differ by more than 1.
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
int l=maxdep(root->left);
int r=maxdep(root->right);
if(l-r<-1||l-r>1) return false;
else returnisBalanced(root->left)&&isBalanced(root->right);
}
private:
int maxdep(TreeNode* root){
if(root==NULL) return 0;
int l=maxdep(root->left);
int r=maxdep(root->right);
return (max(l,r)+1);
}
};
注:递归的典型题
Implement Queue using Stacks
Implement thefollowing operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
class Queue {
public:
// Push element x to the back of queue.
stack<int> s1,s2;
void transfer(){
int x;
if(s2.empty())
while(!s1.empty()){
x=s1.top();
s1.pop();
s2.push(x);
}
}
void push(int x) {
s1.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
transfer();
s2.pop();
}
// Get the front element.
int peek(void) {
int x;
transfer();
x=s2.top();
return x;
}
// Return whether the queue is empty.
bool empty(void) {
if(s1.empty()&&s2.empty()) return true;
else return false;
}
};
注:考察栈和队列的基本操作!!!
Merge Two Sorted Lists
Merge two sortedlinked lists and return it as a new list. The new list should be made bysplicing together the nodes of the first two lists.
Subscribe to see which companiesasked this question
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *head=new ListNode(3);
ListNode *p=head;
while(l1&&l2){
if(l1->val<l2->val){
p->next=l1;
p=l1;
l1=l1->next;
}
else{
p->next=l2;
p=l2;
l2=l2->next;
}
}
while(l1) {
p->next=l1;
p=l1;
l1=l1->next;}
while(l2)
{p->next=l2;
p=l2;
l2=l2->next;}
head=head->next;
return head;
}
};
注:leetcode上的链表,头结点往往带有值,创立一个新的节点,使问题简化很多
Given a binarytree, check whether it is a mirror of itself (ie, symmetric around its center).
For example,this binary tree is symmetric:
1
/\
2 2
/ \/ \
3 44 3
But thefollowing is not:
1
/\
2 2
\ \
3 3
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
return help(root->left,root->right);
}
private:
bool help(TreeNode* left,TreeNode* right){
if(left==NULL&&right==NULL) return true;
if(left==NULL||right==NULL) return false;
if(left->val!=right->val) return false;
return(help(left->right,right->left)&&help(left->left,right->right));
}
};
注:重点!!要注意返回值,if(left->val==right->val)return true; else return false;是错误的!!易犯错误!!
Balanced Binary Tree
Given a binary tree,determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as abinary tree in which the depth of the two subtrees of every node never differ by more than 1.
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
int l=maxdep(root->left);
int r=maxdep(root->right);
if(l-r<-1||l-r>1) return false;
else returnisBalanced(root->left)&&isBalanced(root->right);
}
private:
int maxdep(TreeNode* root){
if(root==NULL) return 0;
int l=maxdep(root->left);
int r=maxdep(root->right);
return (max(l,r)+1);
}
};
注:递归的典型题
Implement Queue using Stacks
Implement thefollowing operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
class Queue {
public:
// Push element x to the back of queue.
stack<int> s1,s2;
void transfer(){
int x;
if(s2.empty())
while(!s1.empty()){
x=s1.top();
s1.pop();
s2.push(x);
}
}
void push(int x) {
s1.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
transfer();
s2.pop();
}
// Get the front element.
int peek(void) {
int x;
transfer();
x=s2.top();
return x;
}
// Return whether the queue is empty.
bool empty(void) {
if(s1.empty()&&s2.empty()) return true;
else return false;
}
};
注:考察栈和队列的基本操作!!!
Merge Two Sorted Lists
Merge two sortedlinked lists and return it as a new list. The new list should be made bysplicing together the nodes of the first two lists.
Subscribe to see which companiesasked this question
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *head=new ListNode(3);
ListNode *p=head;
while(l1&&l2){
if(l1->val<l2->val){
p->next=l1;
p=l1;
l1=l1->next;
}
else{
p->next=l2;
p=l2;
l2=l2->next;
}
}
while(l1) {
p->next=l1;
p=l1;
l1=l1->next;}
while(l2)
{p->next=l2;
p=l2;
l2=l2->next;}
head=head->next;
return head;
}
};
注:leetcode上的链表,头结点往往带有值,创立一个新的节点,使问题简化很多
相关文章推荐
- Unity3D和Lua之间的相互调用
- android 水纹上涨与水滴滴下效果
- Cts框架解析(8)-IBuildProvider
- Quartz1.8.5例子(五)
- git Unstaged changes after reset
- oracle多表查询之内连接,外连接语句总结
- kettle 发送txt邮件
- c语言动态内存分配之正确使用指针
- 【慕课笔记】U2 封装 第7节 JAVA中的静态内部类
- C# 5.0中新增特性
- BUAA 1389:愤怒的DZY【二分】
- 《Effective java》读书记录-第22条-优先考虑静态成员类
- 服务器负载均衡解决方案
- C++函数模板类模板
- SetCapture ReleaseCapture
- 硬盘自动挂载
- 怎么关闭win10系统联机搜索
- 获取系统环境变量
- Mysql常用命令大全
- maven 学习:为什么要使用maven,maven使用过程中的一些参数