Algorithm
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例: 给定二叉树 [3,9,20,null,null,15,7],
3复制代码
/
9 20 / 15 7 返回它的最大深度 3 。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int maxDepth(TreeNode root) { if(root == null){ return 0; } int number = 1 ; int leftDepth = 0,rightDepth = 0; if(root.left != null){ leftDepth = maxDepth(root.left); } if(root.right != null){ rightDepth = maxDepth(root.right); } number += leftDepth > rightDepth ? leftDepth : rightDepth; return number; }}复制代码
Review
The gender gap is also a confidence gap
Tip
之前有个朋友在写sql的时候碰到一个问题写sql查询的时候碰到了问题:
#sql1select u.real_name,u.phone,b.order_no,b.amount as borrow_amount,b.create_time as borrow_time,r.id,r.user_id,r.borrow_id,r.state,r.amount as repay_amount,r.repay_time,r.penalty_amout,r.penalty_day, b.fee ,b.real_amount,channel.name as channel_name,cmro.state as allotState,u.living_img,u.front_img,u.back_img, r.remark from cl_borrow_repay r left join cl_user_base_info u on u.user_id=r.user_id join cl_borrow b on r.borrow_id=b.id left join cl_user user2 on user2.id = u.user_id left join cl_channel channel on user2.channel_id = channel.id left join cl_manual_repay_order cmro on r.id = cmro.borrow_repay_id;#sql2select u.real_name,u.phone,b.order_no,b.amount as borrow_amount,b.create_time as borrow_time,r.id,r.user_id,r.borrow_id,r.state,r.amount as repay_amount,r.repay_time,r.penalty_amout,r.penalty_day, b.fee ,b.real_amount,channel.name as channel_name,cmro.state as allotState,u.living_img,u.front_img,u.back_img, r.remark from cl_borrow_repay r left join cl_user_base_info u on u.user_id=r.user_id join cl_borrow b on r.borrow_id=b.id left join cl_user user2 on user2.id = u.user_id left join cl_channel channel on user2.channel_id = channel.id left join cl_manual_repay_order cmro on r.id = cmro.borrow_repay_idORDER BY r.id desc;复制代码
sql1与sql2上基本(是不是应该用专业点的属于表示)没有改动,仅仅是在末位加上了order by ,但是最后的查询时间有着天壤之别。explain之后的结果如下 sql1:
sql2:发现在Extra中多了Using temporary;Using filesort。 原来查询慢的原因是因为使用orderby 后导致sql查询新增加了临时表及进行了文件排序。大量时间花在这上面了。 那么怎么优化? 1、可以考虑在条件中把主键id加入进去左右一起的条件,因为如果order by 的字段也在where里面这样就不会进入文件排序 2、可以参考一次mysql 优化 (Using temporary ; Using filesort)
Share
JVM发生频繁 CMS GC,罪魁祸首是这个参数! : .