Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0298 #1434

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,21 @@

<!-- 这里可写通用的实现逻辑 -->

DFS。
**方法一:DFS**

我们设计一个函数 $dfs(root)$,表示以 $root$ 为连续序列的第一个节点的最长连续序列路径长度。

函数 $dfs(root)$ 的执行过程如下:

如果 $root$ 为空,那么返回 $0$。

否则,我们递归计算 $root$ 的左右子节点,分别得到 $l$ 和 $r$,如果 $root$ 的左子节点和 $root$ 连续,那么 $l$ 的值加 $1$,否则置 $l$ 为 $1$;如果 $root$ 的右子节点和 $root$ 连续,那么 $r$ 的值加 $1$,否则置 $r$ 为 $1$。

然后我们更新答案为 $ans = \max(ans, l, r)$,并返回 $\max(l, r)$。

最后,我们调用 $dfs(root)$,返回答案 $ans$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。

<!-- tabs:start -->

Expand All @@ -56,18 +70,23 @@ DFS。
# self.left = left
# self.right = right
class Solution:
def longestConsecutive(self, root: TreeNode) -> int:
def dfs(root, p, t):
nonlocal ans
def longestConsecutive(self, root: Optional[TreeNode]) -> int:
def dfs(root: Optional[TreeNode]) -> int:
if root is None:
return
t = t + 1 if p is not None and p.val + 1 == root.val else 1
return 0
l = dfs(root.left) + 1
r = dfs(root.right) + 1
if root.left and root.left.val - root.val != 1:
l = 1
if root.right and root.right.val - root.val != 1:
r = 1
t = max(l, r)
nonlocal ans
ans = max(ans, t)
dfs(root.left, root, t)
dfs(root.right, root, t)
return t

ans = 1
dfs(root, None, 1)
ans = 0
dfs(root)
return ans
```

Expand Down Expand Up @@ -95,19 +114,25 @@ class Solution {
private int ans;

public int longestConsecutive(TreeNode root) {
ans = 1;
dfs(root, null, 1);
dfs(root);
return ans;
}

private void dfs(TreeNode root, TreeNode p, int t) {
private int dfs(TreeNode root) {
if (root == null) {
return;
return 0;
}
int l = dfs(root.left) + 1;
int r = dfs(root.right) + 1;
if (root.left != null && root.left.val - root.val != 1) {
l = 1;
}
t = p != null && p.val + 1 == root.val ? t + 1 : 1;
if (root.right != null && root.right.val - root.val != 1) {
r = 1;
}
int t = Math.max(l, r);
ans = Math.max(ans, t);
dfs(root.left, root, t);
dfs(root.right, root, t);
return t;
}
}
```
Expand All @@ -128,21 +153,27 @@ class Solution {
*/
class Solution {
public:
int ans;

int longestConsecutive(TreeNode* root) {
ans = 1;
dfs(root, nullptr, 1);
int ans = 0;
function<int(TreeNode*)> dfs = [&](TreeNode* root) {
if (!root) {
return 0;
}
int l = dfs(root->left) + 1;
int r = dfs(root->right) + 1;
if (root->left && root->left->val - root->val != 1) {
l = 1;
}
if (root->right && root->right->val - root->val != 1) {
r = 1;
}
int t = max(l, r);
ans = max(ans, t);
return t;
};
dfs(root);
return ans;
}

void dfs(TreeNode* root, TreeNode* p, int t) {
if (!root) return;
t = p != nullptr && p->val + 1 == root->val ? t + 1 : 1;
ans = max(ans, t);
dfs(root->left, root, t);
dfs(root->right, root, t);
}
};
```

Expand All @@ -157,24 +188,26 @@ public:
* Right *TreeNode
* }
*/
func longestConsecutive(root *TreeNode) int {
ans := 1
var dfs func(root, p *TreeNode, t int)
dfs = func(root, p *TreeNode, t int) {
func longestConsecutive(root *TreeNode) (ans int) {
var dfs func(*TreeNode) int
dfs = func(root *TreeNode) int {
if root == nil {
return
return 0
}
l := dfs(root.Left) + 1
r := dfs(root.Right) + 1
if root.Left != nil && root.Left.Val-root.Val != 1 {
l = 1
}
if p != nil && p.Val+1 == root.Val {
t++
ans = max(ans, t)
} else {
t = 1
if root.Right != nil && root.Right.Val-root.Val != 1 {
r = 1
}
dfs(root.Left, root, t)
dfs(root.Right, root, t)
t := max(l, r)
ans = max(ans, t)
return t
}
dfs(root, nil, 1)
return ans
dfs(root)
return
}

func max(a, b int) int {
Expand All @@ -185,6 +218,46 @@ func max(a, b int) int {
}
```

### **TypeScript**

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function longestConsecutive(root: TreeNode | null): number {
let ans = 0;
const dfs = (root: TreeNode | null): number => {
if (root === null) {
return 0;
}
let l = dfs(root.left) + 1;
let r = dfs(root.right) + 1;
if (root.left && root.left.val - root.val !== 1) {
l = 1;
}
if (root.right && root.right.val - root.val !== 1) {
r = 1;
}
const t = Math.max(l, r);
ans = Math.max(ans, t);
return t;
};
dfs(root);
return ans;
}
```

### **...**

```
Expand Down
Loading