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 ...
随机推荐
- Python之字符串函数str()
str()方法使Python将非字符串值表示为字符串: age = 23 message = "Happy" + str(age) +"rd Birthday"
- numpy鸢尾花
import numpy from sklearn.datasets import load_iris #从sklearn包自带的数据集中读出鸢尾花数据集data iris_data = load_i ...
- Google - Largest Sum Submatrix
Given an NxN matrix of positive and negative integers, write code to find the submatrix with the lar ...
- php闭包的使用实例
$childrenNodes = array_filter($list, function($item) use($parentId){ return $item->node_pid == $p ...
- MySQL Transaction--查看未提交事务执行的SQL
未提交事务 长期未提交事务,指开启事务后,长时间未向MySQL发出SQL执行请求或事务处理(COMMIT/ROLLBACK)请求,在系统表`information_schema`.`INNODB_TR ...
- 记一次java电话面试
答案补充中... 一.java基础 1.简述java的几种基本数据类型 JAVA的基本数据类型有:byte.char.boolean.short.int.long.float.double 2.什么是 ...
- DockerFile详解--转载
COPY 复制文件 格式: COPY ... COPY ["",... ""] 和 RUN 指令一样,也有两种格式,一种类似于命令行,一种类似于函数调用. CO ...
- Eclipse从GitHub下载代码
转载自:http://blog.csdn.net/u014785687/article/details/73473769 打开git视图(window->show view),搜索git,选择G ...
- springboot shiro 项目前端页面访问问题总结
1.springboot前端页面默认需要放到指定的目录下才能访问 在/src/main/resource目录下的: /static /public /resources /META-INF/resou ...
- GetPJData - uGetHttpData.pas
function GetPJData(APage: Integer): string; var IdHTTP: TIdHTTP; url: string; paramsList: TStringLis ...