alrogithms
543. Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
1
/ \
2 3
/ \
4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
Solution 1:leetcode上的思路——动态规划
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
max(root);
return max;
}
private int max(TreeNode root){
if(root == null){
return 0;
}
int left = max(root.left);
int right = max(root.right);
max = Math.max(max,left + right);
return Math.max(left,right) + 1;
}
}
review
Regex use vs. Regex abuse
1、如果你经常处理string字符串, 那么正则表达式是你不能抗拒的。
2、正则表达式表现更好,更灵活,代码更好维护。 你可以用一个20字符的正则表达式去代替6行的if…then代码块。还可以通过配置文件去配置正则表达式,不需要重新编译源代码。
获取一个目录的最后一个文件夹:
"[^\\]+\\*$"
3、不要滥用正则表达式。
tips
网络协议第四层:传输层。
UDP(User Datagram Protocol):不可靠的数据报协议,只提供数据的不可靠传递,一旦把应用程序发送给网络层的数据发送出去,就不保留数据备份。UDP的包头只包含端口信息、报文长度和校验和。
UDP的特点:无连接,不可靠传输,缺乏拥塞控制。