注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
附上下面代码:
我看到 locations的实现,implement了comparable,然后override了CompareTo。
至于原因,我想因为是要给Collection.sort()来用。
问题是,Collection.sort() ,sort的对象一定需要CompareTo接口吗?
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Location> locations;
public List<List<Integer>> verticalTraversal(TreeNode root) {
locations = new ArrayList(); /** Each location is a node's x position, y position, and value */
dfs(root, 0, 0); /* Add all nodes to locations*/
Collections.sort(locations);
List<List<Integer>> ans = new ArrayList(); /* final answer container */
ans.add(new ArrayList<Integer>());
int prev = locations.get(0).x; /* x is x-axis */
for (Location loc: locations) {
// If the x value changed, it's part of a new report.
if (loc.x != prev) {
prev = loc.x;
ans.add(new ArrayList<Integer>());
}
// We always add the node's value to the latest report.
ans.get(ans.size() - 1).add(loc.val);
}
return ans;
}
/** DFS **/
public void dfs(TreeNode node, int x, int y) {
if (node != null) {
/** 加入locations **/
locations.add(new Location(x, y, node.val));
dfs(node.left, x-1, y+1);
dfs(node.right, x+1, y+1);
}
}
class Location implements Comparable<Location>{
int x, y, val;
Location(int x, int y, int val) {
this.x = x;
this.y = y;
this.val = val;
}
@Override
public int compareTo(Location that) {
if (this.x != that.x)
return Integer.compare(this.x, that.x);
else if (this.y != that.y)
return Integer.compare(this.y, that.y);
else
return Integer.compare(this.val, that.val);
}
}
} |