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.0987 #2341

Merged
merged 1 commit into from
Feb 13, 2024
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 @@ -62,7 +62,13 @@

## 解法

### 方法一
### 方法一:DFS + 排序

我们设计一个函数 $dfs(root, i, j)$,其中 $i$ 和 $j$ 表示当前节点的行和列。我们可以通过深度优先搜索的方式,将节点的行和列信息记录下来,存储在一个数组或列表 $nodes$ 中,然后对 $nodes$ 按照列、行、值的顺序进行排序。

接着,我们遍历 $nodes$,将相同列的节点值放到同一个列表中,最后返回这些列表。

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

<!-- tabs:start -->

Expand All @@ -74,60 +80,157 @@
# self.left = left
# self.right = right
class Solution:
def verticalTraversal(self, root: TreeNode) -> List[List[int]]:
def dfs(root, i, j):
def verticalTraversal(self, root: Optional[TreeNode]) -> List[List[int]]:
def dfs(root: Optional[TreeNode], i: int, j: int):
if root is None:
return
nodes.append((i, j, root.val))
nodes.append((j, i, root.val))
dfs(root.left, i + 1, j - 1)
dfs(root.right, i + 1, j + 1)

nodes = []
dfs(root, 0, 0)
nodes.sort(key=lambda x: (x[1], x[0], x[2]))
nodes.sort()
ans = []
prev = -2000
for i, j, v in nodes:
for j, _, val in nodes:
if prev != j:
ans.append([])
prev = j
ans[-1].append(v)
ans[-1].append(val)
return ans
```

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<int[]> nodes = new ArrayList<>();

public List<List<Integer>> verticalTraversal(TreeNode root) {
List<int[]> list = new ArrayList<>();
dfs(root, 0, 0, list);
list.sort(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] != o2[0]) return Integer.compare(o1[0], o2[0]);
if (o1[1] != o2[1]) return Integer.compare(o2[1], o1[1]);
return Integer.compare(o1[2], o2[2]);
dfs(root, 0, 0);
Collections.sort(nodes, (a, b) -> {
if (a[0] != b[0]) {
return Integer.compare(a[0], b[0]);
}
if (a[1] != b[1]) {
return Integer.compare(a[1], b[1]);
}
return Integer.compare(a[2], b[2]);
});
List<List<Integer>> res = new ArrayList<>();
int preX = 1;
for (int[] cur : list) {
if (preX != cur[0]) {
res.add(new ArrayList<>());
preX = cur[0];
List<List<Integer>> ans = new ArrayList<>();
int prev = -2000;
for (int[] node : nodes) {
int j = node[0], val = node[2];
if (prev != j) {
ans.add(new ArrayList<>());
prev = j;
}
res.get(res.size() - 1).add(cur[2]);
ans.get(ans.size() - 1).add(val);
}
return res;

return ans;
}

private void dfs(TreeNode root, int x, int y, List<int[]> list) {
private void dfs(TreeNode root, int i, int j) {
if (root == null) {
return;
}
list.add(new int[] {x, y, root.val});
dfs(root.left, x - 1, y - 1, list);
dfs(root.right, x + 1, y - 1, list);
nodes.add(new int[] {j, i, root.val});
dfs(root.left, i + 1, j - 1);
dfs(root.right, i + 1, j + 1);
}
}
```

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> verticalTraversal(TreeNode* root) {
vector<tuple<int, int, int>> nodes;
function<void(TreeNode*, int, int)> dfs = [&](TreeNode* root, int i, int j) {
if (!root) {
return;
}
nodes.emplace_back(j, i, root->val);
dfs(root->left, i + 1, j - 1);
dfs(root->right, i + 1, j + 1);
};
dfs(root, 0, 0);
sort(nodes.begin(), nodes.end());
vector<vector<int>> ans;
int prev = -2000;
for (auto [j, _, val] : nodes) {
if (j != prev) {
prev = j;
ans.emplace_back();
}
ans.back().push_back(val);
}
return ans;
}
};
```
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func verticalTraversal(root *TreeNode) (ans [][]int) {
nodes := [][3]int{}
var dfs func(*TreeNode, int, int)
dfs = func(root *TreeNode, i, j int) {
if root == nil {
return
}
nodes = append(nodes, [3]int{j, i, root.Val})
dfs(root.Left, i+1, j-1)
dfs(root.Right, i+1, j+1)
}
dfs(root, 0, 0)
sort.Slice(nodes, func(i, j int) bool {
a, b := nodes[i], nodes[j]
return a[0] < b[0] || a[0] == b[0] && (a[1] < b[1] || a[1] == b[1] && a[2] < b[2])
})
prev := -2000
for _, node := range nodes {
j, val := node[0], node[2]
if j != prev {
ans = append(ans, nil)
prev = j
}
ans[len(ans)-1] = append(ans[len(ans)-1], val)
}
return
}
```

Expand All @@ -147,41 +250,28 @@ class Solution {
*/

function verticalTraversal(root: TreeNode | null): number[][] {
let solution = [];
dfs(root, 0, 0, solution);
// 优先依据i=2排序, 然后依据i=1排序
solution.sort(compare);
let ans = [];
let pre = Number.MIN_SAFE_INTEGER;
for (let node of solution) {
const [val, , idx] = node;
if (idx != pre) {
const nodes: [number, number, number][] = [];
const dfs = (root: TreeNode | null, i: number, j: number) => {
if (!root) {
return;
}
nodes.push([j, i, root.val]);
dfs(root.left, i + 1, j - 1);
dfs(root.right, i + 1, j + 1);
};
dfs(root, 0, 0);
nodes.sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]);
const ans: number[][] = [];
let prev = -2000;
for (const [j, _, val] of nodes) {
if (j !== prev) {
prev = j;
ans.push([]);
pre = idx;
}
ans[ans.length - 1].push(val);
ans.at(-1)!.push(val);
}
return ans;
}

function compare(a: Array<number>, b: Array<number>) {
const [a0, a1, a2] = a,
[b0, b1, b2] = b;
if (a2 == b2) {
if (a1 == b1) {
return a0 - b0;
}
return a1 - b1;
}
return a2 - b2;
}

function dfs(root: TreeNode | null, depth: number, idx: number, solution: Array<Array<number>>) {
if (!root) return;
solution.push([root.val, depth, idx]);
dfs(root.left, depth + 1, idx - 1, solution);
dfs(root.right, depth + 1, idx + 1, solution);
}
```

<!-- tabs:end -->
Expand Down
Loading
Loading