Algorithm
本周选择的算法题是:Balanced Binary Tree。
规则
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: true
Example 2:
Input: root = [1,2,2,3,3,null,null,4,4]
Output: false
Example 3:
Input: root = []
Output: true
Constraints:
- The number of nodes in the tree is in the range
[0, 5000]
. -104 <= Node.val <= 104
Solution
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
def check(node: TreeNode):
if not node: return 0
left, right = check(node.left), check(node.right)
if left == -1 or right == -1 or abs(left - right) > 1:
return -1
else:
return 1 + max(left, right)
return check(root) != -1
Review
Can Java microservices be as fast as Go?
Java 微服务能否像 Go 那样快?作者的结论如下:
- Kubernetes does not seem to scale out quickly
- Java seems to be better at using all available cores/threads than Go — we saw much better CPU utilization during Java tests
- Java performance was better on machines with more cores and memory, Go performance was better on smaller/less powerful machines
- Go performance was overall slightly more consistent — probably due to Java’s garbage collection
- On a “production-sized” machine Java was easily as fast as Go, or faster
- Logging seemed to be the main bottleneck we encountered in both Go and Java
- Modern versions of Java, and new frameworks like Helidon, are making large strides in removing/reducing the pain of some of Java’s well-known and long established issues (e.g. verbosity, GC performance, start up time, etc.)
Java 从 1996 年 1.0 到如今的 16.0 走过了 25 年的历史,仍然是当前最流行的语言之一。我本来期望从作者的文章中了解一些让 Java 更快的 “tricks”,但似乎并没有从中得到太多信息。
可以预见 Java 还会存在很长一段时间,它生态比 Go 更成熟,已有项目也多,但对于新项目来说,显然有更好的选择,本文并没有让 Go 真正发挥出高性能,而且有些测试没有使用日志,真实的线上项目是不可能没有日志的,哪怕会影响性能。我们可以看到过去几年很多微服务都转向了 Go,Go 的开发周期更短,错误更少,响应时间更快,不用重新启动长时间运行的进程,未来 Go 和 Rust 等新的语言可能真的会结束 Java 时代。
Tip
更新了 CodingTour 关于 navbar 的样式,解决 title 不能选取的问题。