leetcode每日刷题计划-简单篇day10
跳题,不熟悉的留到周末再做。
保持冷静,不信画饼。
num 100 相同的树 Same Tree
做法可能不是特别简洁,注意一下。最后判断完子树以后,要确定根的数值是一样的
然后在isleaf的判定先要确定NULL
/**
* 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:
bool isleaf(TreeNode*p)
{
if (p==NULL) return false;
if(p->left==NULL && p->right==NULL)
return true;
else
return false;
}
bool isSameTree(TreeNode* p, TreeNode* q) {
if(isleaf(p) && isleaf(q))
{
if(p->val==q->val)
return true;
else
return false;
}
else
{
if(isleaf(p) && !isleaf(q))
return false;
if(!isleaf(p) && isleaf(q))
return false;
if(p==NULL && q!=NULL)
return false;
if(p!=NULL && q==NULL)
return false;
if(p==NULL && q==NULL)
return true;
if((p->val==q->val) && isSameTree(p->left,q->left) && isSameTree(p->right,q->right))
return true;
else return false;
}
return false;
}
};
num 104 二叉树的最大深度 Maximum Depth of Binary Tree
注意判断root==NULL
/**
* 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:
int maxDepth(TreeNode* root) {
if(root==NULL)
return ;
if(root->left==NULL && root->right==NULL)
return ;
if(root->left==NULL)
return(maxDepth(root->right)+);
if(root->right==NULL)
return (maxDepth(root->left)+);
else
return (max(maxDepth(root->left),maxDepth(root->right))+);
}
};
leetcode每日刷题计划-简单篇day10的更多相关文章
- leetcode每日刷题计划-简单篇day5
刷题成习惯以后感觉挺好的 Num 27 移除元素 Remove Element 跟那个排序去掉相同的一样,len标记然后新的不重复的直接放到len class Solution { public: i ...
- leetcode每日刷题计划-简单篇day3
收到swe提前批面试hhh算是ep挂了的后续 努力刷题呀争取今年冲进去! Num 21 合并两个有序链表 Merge Two Sorted Lists 注意新开的链表用来输出结果的是ListNode ...
- leetcode每日刷题计划-简单篇day1
orzorz开始刷题 争取坚持每周平均下来简单题一天能做两道题吧 非常简单的题奇奇怪怪的错误orz越不做越菜 Num 7 整数反转 Reverse Integer 刚开始多给了一个变量来回折腾占地方, ...
- leetcode每日刷题计划-简单篇day12
Num 125 验证回文串 Valid Palindrome 非常有收货的一道题嘻嘻嘻,本来是考试期间划水挑的题,坑点有点多 第一个是注意对temp1和temp2中途更新的判断 第二个是字符串频繁的作 ...
- leetcode每日刷题计划-简单篇day9
Num 38 报数 Count and Say 题意读起来比较费劲..看懂了题还是不难的 注意最后的长度是sz的长度,开始写错写的len 在下次计算的时候len要更新下 说明 直接让char和int进 ...
- leetcode每日刷题计划-简单篇day6
突发奇想&胡思乱想的一天 银行家算法证明错了并挂在黑板上的可怜希希 Num 53 最大子序和 Maximum Subarray O(n)的算法实现了,分治法有空补 class Solution ...
- leetcode每日刷题计划-简单篇day2
今天数模比赛爆肝&操作系统大作业 脖子疼orz先把题过了保证flag不倒..个别细节回头看吧 Num 13 罗马数字转整数 Roman to Integer 一遍提交过,开始编译出了点问题 具 ...
- leetcode每日刷题计划-简单篇day13
Num 169 先码,回头再说,摩尔算法... tle了 class Solution { public: int majorityElement(vector<int>& num ...
- leetcode每日刷题计划-简单篇day11
Num 121 买卖股票的最佳时期 Best Time to Buy and Sell Stock class Solution { public: int maxProfit(vector<i ...
随机推荐
- 小妖精的完美游戏教室——buff系统
作者:小妖精Balous,未经作者允许,任何个人与单位不得将此源代码用于商业化项目 #region buff /// <summary> /// 是否魔法免疫,魔法免疫的生物不会受到除自己 ...
- 作业二、comp和swap函数
一.swap函数的代码及运行情况 1.代码 #include<stdio.h> int main() { void swap(int *m,int *n); int a,b; int *p ...
- [USACO06JAN]树林The Grove
树木(grove)Time Limit: 1Sec Memory Limit: 64 MB[Description]牧场里有一片树林,林子里没有坑.贝茜很想知道,最少需要多少步能围绕树林走一圈,最后回 ...
- IPFS 探索
IPFS 探索 比特币当前是用于存金融交易数据,有leveldb 存关键小的交易数据.那么我们的文件,譬如一个网站里面的static file 怎么办? IPFS(InterPlanetary Fil ...
- 【mysql-server】遇到的坑
一.前提 最近因为工作原因,不得不用windows电脑,发现windows装mysql真的坑太多 23333: 二.环境 windows 环境 mysql 5.7(不建议用5.8坑更多) 三.步骤 3 ...
- Setting
http://www.cnblogs.com/felixwang2/p/8798653.html
- ansible常用模块入门
常用模块有以下几个 command copy shell crond yum service setup 1.command模块 ansible george -m command -a " ...
- 如何编译luabind支持vs2010之后所有版本
步骤 下载https://github.com/luabind/luabind/tree/0.9. 其最后一次commit为 Revision: 8c66030818f0eacbb7356c16776 ...
- .NET MVC 表主外键关系 JSON 无限循环 方案二(推荐)
public class JsonResultObject:JsonResult { private Newtonsoft.Json.JsonSerializerSettings Settings { ...
- Azure CosmosDB (10) Azure Cosmos DB体系结构
<Windows Azure Platform 系列文章目录> Azure Cosmos DB的体系结构分为以下几个部分: 1.Database Accounts Database Acc ...