Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8824e11

Browse files
20200912
1 parent 55f7baf commit 8824e11

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @lc app=leetcode id=199 lang=java
3+
*
4+
* [199] Binary Tree Right Side View
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
/*
25+
类似preorder, 但是仅当level = res.size()时,之前level都加上了目标node,此时加上的root就是right view的第一个
26+
之后每次先向右边递归就能得到每层的right view
27+
time: O(h)
28+
space: O(h)
29+
*/
30+
public List<Integer> rightSideView(TreeNode root) {
31+
List<Integer> res = new ArrayList<>();
32+
if (root == null) return res;
33+
rightView(root, res, 0);
34+
return res;
35+
}
36+
37+
private void rightView(TreeNode root, List<Integer> res, int level) {
38+
if (root == null) return;
39+
if (level == res.size()) res.add(root.val);
40+
rightView(root.right, res, level + 1);
41+
rightView(root.left, res, level + 1);
42+
}
43+
}
44+
// @lc code=end
45+

‎Java/216.combination-sum-iii.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @lc app=leetcode id=216 lang=java
3+
*
4+
* [216] Combination Sum III
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
dfs
11+
time: O(n*2^n) n = 9
12+
sapce: O(n)
13+
*/
14+
public List<List<Integer>> combinationSum3(int k, int n) {
15+
List<List<Integer>> res = new ArrayList<>();
16+
dfs(res, new ArrayList<>(), k, n, 1);
17+
return res;
18+
}
19+
20+
private void dfs(List<List<Integer>> res, List<Integer> cur, int k, int n, int pos) {
21+
if (n <= 0 || cur.size() == k) {
22+
if (cur.size() == k && n == 0) {
23+
res.add(new ArrayList<>(cur));
24+
}
25+
return;
26+
}
27+
for (int i = pos; i <= 9; ++i) {
28+
cur.add(i);
29+
dfs(res, cur, k, n - i, i + 1);
30+
cur.remove(cur.size() - 1);
31+
}
32+
}
33+
}
34+
// @lc code=end
35+

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
## Tree
164164
| Problem | Solution
165165
:- | :-:
166+
[199. Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) | [preorder + level check](https://github.com/Yukinichi/leetcode/blob/master/Java/199.binary-tree-right-side-view.java)
166167
[662. Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/) | [Solution](https://github.com/Yukinichi/leetcode/blob/master/Java/662.maximum-width-of-binary-tree.java)
167168
[1110. Delete Nodes And Return Forest](https://leetcode.com/problems/delete-nodes-and-return-forest/) | [Solution](https://github.com/Yukinichi/leetcode/blob/master/Java/1110.delete-nodes-and-return-forest.java)
168169

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /