Skip to content

Commit e7a9abd

Browse files
three solution for binary tree dfs
1 parent 25cce61 commit e7a9abd

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
|1029|[Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/) | |Easy|
1717
|1028|[Recover a Tree From Preorder Traversal](https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/) | |Hard|
1818
|1024|[Video Stitching](https://leetcode.com/problems/video-stitching/) | |Medium|
19-
|993|[Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | |Easy|
19+
|993|[Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [java](./algorithm/cousinsInBinaryTree/solution.java) |Easy|
2020
|991|[Broken Calculator](https://leetcode.com/problems/broken-calculator/) | |Medium|
2121
|990|[Satisfiability of Equality Equations](https://leetcode.com/problems/satisfiability-of-equality-equations/) | |Medium|
2222
|989|[Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | |Easy|
@@ -35,6 +35,7 @@
3535
|976|[Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/) | |Easy|
3636
|971|[Flip Binary Tree To Match Preorder Traversal](https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/) | |Medium|
3737
|969|[Pancake Sorting](https://leetcode.com/problems/pancake-sorting/) | |Medium|
38+
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)| [Java](./algorithms/uniValuedBinaryTree/Solution.java) |Easy|
3839
|961|[N-Repeated element in size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) | |Easy|
3940
|958|[Check Completeness of a Binary Tree](https://leetcode.com/problems/check-completeness-of-a-binary-tree/) | |Medium|
4041
|951|[Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/) | |Medium|
@@ -80,7 +81,7 @@
8081
|626|[Exchange Seats](https://leetcode.com/problems/exchange-seats/) | [Mysql](./algorithm/swapSalary/Solution.java) | |Easy|
8182
|623|[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | |Medium|
8283
|620|[Not Boring Movies](https://leetcode.com/problems/not-boring-movies/)| [Mysql](./algorithms/notBoringMovies/Solution.sql) |Easy|
83-
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)| [Java](./algorithms/mergeTwoBinaryTrees/Solution.java)
84+
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)| [Java](./algorithms/mergeTwoBinaryTrees/Solution.java) |Easy|
8485
|595|[Big Countries](https://leetcode.com/problems/big-countries/)| [Mysql](./algorithms/bigCountries/Solution.sql) |Easy|
8586
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/) | |Easy|
8687
|575|[Distrubute Candies](https://leetcode.com/problems/distribute-candies/)| [java](./algorithms/distributeCandies/Solution.java) |Easy|
@@ -199,7 +200,7 @@
199200
|262|[Trips and Users](https://leetcode.com/problems/trips-and-users/)| [Mysql](./algorithms/tripsAndUsers/Solution.sql) |Hard|
200201
|260|[Single Number III](https://leetcode.com/problems/single-number-iii/)| |Medium|
201202
|258|[Add Digits](https://leetcode.com/problems/add-digits/)| |Easy|
202-
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| |Easy|
203+
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [Java](./algorithms/binaryTreePaths/solution.java) |Easy|
203204
|242|[Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [js](./algorithms/validAnagram/validAnagram.js) |Easy|
204205
|241|[Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/)||Medium|
205206
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)||Medium|
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public List<String> binaryTreePaths(TreeNode root) {
12+
List<String> paths = new ArrayList<String>();
13+
constructPaths(root, "", paths);
14+
return paths;
15+
}
16+
17+
//深度遍历就是先递归查找某一路径下所有叶子节点
18+
public void constructPaths(TreeNode root, String path, List<String> paths) {
19+
if (root != null) {
20+
StringBuffer pathSB = new StringBuffer(path);
21+
pathSB.append(Integer.toString(root.val));
22+
if (root.left == null && root.right == null) { // 当前节点是叶子节点
23+
paths.add(pathSB.toString()); // 把路径加入到答案中
24+
} else {
25+
pathSB.append("->"); // 当前节点不是叶子节点,继续递归遍历
26+
constructPaths(root.left, pathSB.toString(), paths);
27+
constructPaths(root.right, pathSB.toString(), paths);
28+
}
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
Map<Integer, Integer> depth;
18+
Map<Integer, TreeNode> parent;
19+
20+
public boolean isCousins(TreeNode root, int x, int y) {
21+
depth = new HashMap();
22+
parent = new HashMap();
23+
dfs(root, null);
24+
return (depth.get(x) == depth.get(y) && parent.get(x) != parent.get(y));
25+
}
26+
27+
//深度遍历
28+
public void dfs(TreeNode node, TreeNode par) {
29+
if (node != null) {
30+
depth.put(node.val, par != null ? 1 + depth.get(par.val) : 0);
31+
parent.put(node.val, par);
32+
dfs(node.left, node);
33+
dfs(node.right, node);
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public boolean isUnivalTree(TreeNode root) {
12+
Set<Integer> values = new HashSet();
13+
dfs(root,values);
14+
return values.size() == 1;
15+
}
16+
17+
//深度遍历
18+
private void dfs(TreeNode root,Set<Integer> values){
19+
if(root!=null){
20+
values.add(root.val);
21+
dfs(root.left,values);
22+
dfs(root.right,values);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)