Skip to content

Commit 7bf0c26

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions with tage tree.
1 parent f698861 commit 7bf0c26

5 files changed

+319
-4
lines changed

leetcode/102. Binary Tree Level Order Traversal.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,38 @@ class Solution {
9191
return result;
9292
}
9393
}
94-
```
94+
```
95+
96+
### Third Time
97+
* Method 1: bfs
98+
```Java
99+
/**
100+
* Definition for a binary tree node.
101+
* public class TreeNode {
102+
* int val;
103+
* TreeNode left;
104+
* TreeNode right;
105+
* TreeNode(int x) { val = x; }
106+
* }
107+
*/
108+
class Solution {
109+
public List<List<Integer>> levelOrder(TreeNode root) {
110+
List<List<Integer>> result = new ArrayList<>();
111+
if(root == null) return result;
112+
Queue<TreeNode> q = new LinkedList<>();
113+
q.offer(root);
114+
while(!q.isEmpty()){
115+
List<Integer> temp = new ArrayList<>();
116+
int size = q.size();
117+
for(int i = 0; i < size; i++){
118+
TreeNode node = q.poll();
119+
temp.add(node.val);
120+
if(node.left != null) q.offer(node.left);
121+
if(node.right != null) q.offer(node.right);
122+
}
123+
result.add(temp);
124+
}
125+
return result;
126+
}
127+
}
128+
```

leetcode/107. Binary Tree Level Order Traversal II.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,39 @@ class Solution {
9292
return result;
9393
}
9494
}
95-
```
95+
```
96+
97+
### Third Time
98+
* Method 1: BFS
99+
```Java
100+
/**
101+
* Definition for a binary tree node.
102+
* public class TreeNode {
103+
* int val;
104+
* TreeNode left;
105+
* TreeNode right;
106+
* TreeNode(int x) { val = x; }
107+
* }
108+
*/
109+
class Solution {
110+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
111+
List<List<Integer>> result = new ArrayList<>();
112+
if(root == null) return result;
113+
Queue<TreeNode> q = new LinkedList<>();
114+
q.offer(root);
115+
while(!q.isEmpty()){
116+
List<Integer> temp = new ArrayList<>();
117+
int size = q.size();
118+
for(int i = 0; i < size; i++){
119+
TreeNode node = q.poll();
120+
temp.add(node.val);
121+
if(node.left != null) q.offer(node.left);
122+
if(node.right != null) q.offer(node.right);
123+
}
124+
result.add(temp);
125+
}
126+
Collections.reverse(result);
127+
return result;
128+
}
129+
}
130+
```

leetcode/297. Serialize and Deserialize Binary Tree.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public class Codec {
9393
public class Codec {
9494
private final String split = ",";
9595
private final String empty = "#";
96-
96+
9797
// Encodes a tree to a single string.
9898
public String serialize(TreeNode root) {
9999
StringBuilder sb = new StringBuilder();
@@ -131,4 +131,57 @@ public class Codec {
131131
// Your Codec object will be instantiated and called as such:
132132
// Codec codec = new Codec();
133133
// codec.deserialize(codec.serialize(root));
134-
```
134+
```
135+
136+
### Third Time
137+
* Method 1: dfs
138+
```Java
139+
/**
140+
* Definition for a binary tree node.
141+
* public class TreeNode {
142+
* int val;
143+
* TreeNode left;
144+
* TreeNode right;
145+
* TreeNode(int x) { val = x; }
146+
* }
147+
*/
148+
public class Codec {
149+
private static final String EMPTY = "#";
150+
private static final String SPLIT = ",";
151+
// Encodes a tree to a single string.
152+
public String serialize(TreeNode root) {
153+
StringBuilder sb = new StringBuilder();
154+
serialize(root, sb);
155+
return sb.toString();
156+
}
157+
private void serialize(TreeNode node, StringBuilder sb){
158+
if(node == null){
159+
sb.append(EMPTY).append(SPLIT);
160+
}else{
161+
sb.append(node.val).append(SPLIT);
162+
serialize(node.left, sb);
163+
serialize(node.right, sb);
164+
}
165+
}
166+
// Decodes your encoded data to tree.
167+
public TreeNode deserialize(String data) {
168+
String[] tokens = data.split(SPLIT);
169+
LinkedList<String> q = new LinkedList<>();
170+
q.addAll(Arrays.asList(tokens));
171+
return deserialize(q);
172+
}
173+
private TreeNode deserialize(LinkedList<String> q){
174+
String token = q.poll();
175+
if(token.equals(EMPTY)) return null;
176+
else{
177+
TreeNode node = new TreeNode(Integer.parseInt(token));
178+
node.left = deserialize(q);
179+
node.right = deserialize(q);
180+
return node;
181+
}
182+
}
183+
}
184+
// Your Codec object will be instantiated and called as such:
185+
// Codec codec = new Codec();
186+
// codec.deserialize(codec.serialize(root));
187+
```
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## 449. Serialize and Deserialize BST
2+
3+
### Question
4+
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
5+
6+
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.
7+
8+
The encoded string should be as compact as possible.
9+
10+
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
11+
12+
### Solution:
13+
* Method 1: dfs
14+
```Java
15+
/**
16+
* Definition for a binary tree node.
17+
* public class TreeNode {
18+
* int val;
19+
* TreeNode left;
20+
* TreeNode right;
21+
* TreeNode(int x) { val = x; }
22+
* }
23+
*/
24+
public class Codec {
25+
private static final String empty = "#";
26+
private static final String split = ",";
27+
// Encodes a tree to a single string.
28+
public String serialize(TreeNode root) {
29+
StringBuilder sb = new StringBuilder();
30+
serialize(root, sb);
31+
return sb.toString();
32+
}
33+
private void serialize(TreeNode node, StringBuilder sb){
34+
if(node == null){
35+
sb.append(empty).append(split);
36+
}else{
37+
sb.append(node.val).append(split);
38+
serialize(node.left, sb);
39+
serialize(node.right, sb);
40+
}
41+
}
42+
// Decodes your encoded data to tree.
43+
public TreeNode deserialize(String data) {
44+
String[] tokens = data.split(split);
45+
LinkedList<String> q = new LinkedList<>();
46+
q.addAll(Arrays.asList(tokens));
47+
return deserialize(q);
48+
}
49+
private TreeNode deserialize(Queue<String> q){
50+
String token = q.poll();
51+
if(token.equals(empty)) return null;
52+
else{
53+
TreeNode node = new TreeNode(Integer.parseInt(token));
54+
node.left = deserialize(q);
55+
node.right = deserialize(q);
56+
return node;
57+
}
58+
}
59+
}
60+
61+
// Your Codec object will be instantiated and called as such:
62+
// Codec codec = new Codec();
63+
// codec.deserialize(codec.serialize(root));
64+
```
65+
66+
* Method 2: Use the property of BST
67+
```Java
68+
/**
69+
* Definition for a binary tree node.
70+
* public class TreeNode {
71+
* int val;
72+
* TreeNode left;
73+
* TreeNode right;
74+
* TreeNode(int x) { val = x; }
75+
* }
76+
*/
77+
public class Codec {
78+
79+
// Encodes a tree to a single string.
80+
public String serialize(TreeNode root) {
81+
StringBuilder sb = new StringBuilder();
82+
dfs(root, sb);
83+
return sb.toString();
84+
}
85+
private void dfs(TreeNode node, StringBuilder sb){
86+
if(node == null) return;
87+
else{
88+
sb.append(node.val).append(",");
89+
dfs(node.left, sb);
90+
dfs(node.right, sb);
91+
}
92+
}
93+
94+
// Decodes your encoded data to tree.
95+
public TreeNode deserialize(String data) {
96+
if(data.trim().length() == 0) return null;
97+
String[] tokens = data.split(",");
98+
TreeNode root = new TreeNode(Integer.parseInt(tokens[0]));
99+
for(int i = 1; i < tokens.length; i++){
100+
insert(root, Integer.parseInt(tokens[i]));
101+
}
102+
return root;
103+
}
104+
private TreeNode insert(TreeNode node, int num){
105+
if(node == null) return new TreeNode(num);
106+
else if(num < node.val || node.val == num){
107+
node.left = insert(node.left, num);
108+
}else if(num > node.val){
109+
node.right = insert(node.right, num);
110+
}
111+
return node;
112+
}
113+
}
114+
115+
// Your Codec object will be instantiated and called as such:
116+
// Codec codec = new Codec();
117+
// codec.deserialize(codec.serialize(root));
118+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## 508. Most Frequent Subtree Sum
2+
3+
### Question
4+
Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.
5+
6+
```
7+
Examples 1
8+
Input:
9+
10+
5
11+
/ \
12+
2 -3
13+
return [2, -3, 4], since all the values happen only once, return all of them in any order.
14+
Examples 2
15+
Input:
16+
17+
5
18+
/ \
19+
2 -5
20+
return [2], since 2 happens twice, however -5 only occur once.
21+
```
22+
23+
Note: You may assume the sum of values in any subtree is in the range of 32-bit signed integer.
24+
25+
### Solution
26+
* Method 1: dfs + Map
27+
```Java
28+
/**
29+
* Definition for a binary tree node.
30+
* public class TreeNode {
31+
* int val;
32+
* TreeNode left;
33+
* TreeNode right;
34+
* TreeNode(int x) { val = x; }
35+
* }
36+
*/
37+
class Solution {
38+
private Map<Integer, Integer> map;
39+
private int max;
40+
public int[] findFrequentTreeSum(TreeNode root) {
41+
this.map = new HashMap<>();
42+
if(root == null) return new int[]{};
43+
getSubtreeSum(root);
44+
List<Integer> res = new ArrayList<>();
45+
for(Map.Entry<Integer, Integer> entry: map.entrySet()){
46+
if(entry.getValue() == max){
47+
res.add(entry.getKey());
48+
}
49+
}
50+
int size = res.size();
51+
int index = 0;
52+
int[] result = new int[size];
53+
for(Integer n : res)
54+
result[index++] = n;
55+
return result;
56+
}
57+
private int getSubtreeSum(TreeNode node){
58+
if(node.left == null && node.right == null){
59+
int appear = map.containsKey(node.val) ? map.get(node.val): 0;
60+
map.put(node.val, appear + 1);
61+
max = Math.max(max, appear + 1);
62+
return node.val;
63+
}else{
64+
int left = 0, right = 0;
65+
if(node.left != null) left = getSubtreeSum(node.left);
66+
if(node.right != null) right = getSubtreeSum(node.right);
67+
int sum = node.val + left + right;
68+
int appearSum = map.containsKey(sum) ? map.get(sum): 0;
69+
map.put(sum, appearSum + 1);
70+
max = Math.max(max, appearSum + 1);
71+
return sum;
72+
}
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)