Open Zheaoli opened 2 years ago
class Solution {
int ans = 0;
public int longestUnivaluePath(TreeNode root) {
dfs(root, -1001);
return ans;
}
private int dfs(TreeNode root, int val) {
if (root == null) {
return 0;
}
int left = dfs(root.left, root.val), right = dfs(root.right, root.val);
ans = Math.max(left + right, ans);
if (root.val == val) {
return Math.max(left, right) + 1;
}
return 0;
}
}
WeChat: Saraad
/*
* @lc app=leetcode id=191 lang=typescript
*
* [191] Number of 1 Bits
*/
// @lc code=start
function hammingWeight(n: number): number {
let count = 0;
while (n) {
if (n & 1) {
count++;
}
n = n >>> 1;
}
return count;
};
// @lc code=end
微信id: 弘树 来自 vscode 插件
2022-09-02