forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeNode.java
50 lines (42 loc) · 1.21 KB
/
TreeNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.fishercoder.common.classes;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TreeNode)) {
return false;
}
TreeNode treeNode = (TreeNode) o;
if (val != treeNode.val) {
return false;
}
if (left != null ? !left.equals(treeNode.left) : treeNode.left != null) {
return false;
}
return right != null ? right.equals(treeNode.right) : treeNode.right == null;
}
@Override
public int hashCode() {
int result = val;
result = 31 * result + (left != null ? left.hashCode() : 0);
result = 31 * result + (right != null ? right.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "TreeNode{" + "val=" + val + ", left=" + left + ", right=" + right + '}';
}
public TreeNode(int x) {
this.val = x;
}
public TreeNode(TreeNode left, int val, TreeNode right) {
this.left = left;
this.val = val;
this.right = right;
}
}