Movatterモバイル変換


[0]ホーム

URL:


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

Commit699bd2e

Browse files
committed
Added 5 solutions
1 parent680588b commit699bd2e

5 files changed

+254
-0
lines changed
Lines changed: 36 additions & 0 deletions
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(int x) { val = x; }
8+
* }
9+
*/
10+
classSolution {
11+
intans =0;
12+
doubleminDiff =Double.MAX_VALUE;
13+
14+
publicintclosestValue(TreeNoderoot,doubletarget) {
15+
updateVal(root,target);
16+
returnans;
17+
}
18+
19+
privatevoidupdateVal(TreeNoderoot,doubletarget) {
20+
if (root ==null) {
21+
return;
22+
}
23+
24+
if (Math.abs(root.val -target) <minDiff) {
25+
minDiff =Math.abs(root.val -target);
26+
ans =root.val;
27+
}
28+
29+
if (root.val <target) {
30+
updateVal(root.right,target);
31+
}
32+
else {
33+
updateVal(root.left,target);
34+
}
35+
}
36+
}

‎Easy/Nested List Weight Sum.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* public interface NestedInteger {
5+
* // Constructor initializes an empty nested list.
6+
* public NestedInteger();
7+
*
8+
* // Constructor initializes a single integer.
9+
* public NestedInteger(int value);
10+
*
11+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
12+
* public boolean isInteger();
13+
*
14+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
15+
* // Return null if this NestedInteger holds a nested list
16+
* public Integer getInteger();
17+
*
18+
* // Set this NestedInteger to hold a single integer.
19+
* public void setInteger(int value);
20+
*
21+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
22+
* public void add(NestedInteger ni);
23+
*
24+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
25+
* // Return null if this NestedInteger holds a single integer
26+
* public List<NestedInteger> getList();
27+
* }
28+
*/
29+
classSolution {
30+
intsum =0;
31+
32+
publicintdepthSum(List<NestedInteger>nestedList) {
33+
for (NestedIntegern :nestedList) {
34+
dfsHelper(n,1);
35+
}
36+
37+
returnsum;
38+
}
39+
40+
privatevoiddfsHelper(NestedIntegern,intdepth) {
41+
if (n.isInteger()) {
42+
sum +=n.getInteger() *depth;
43+
}
44+
else {
45+
for (NestedIntegeri :n.getList()) {
46+
dfsHelper(i,depth+1);
47+
}
48+
}
49+
}
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
classSolution {
11+
publicList<List<Integer>>findLeaves(TreeNoderoot) {
12+
List<List<Integer>>list =newArrayList<>();
13+
14+
while (root !=null) {
15+
List<Integer>leaves =getLeaves(root);
16+
root =removeLeaves(root);
17+
list.add(leaves);
18+
}
19+
20+
returnlist;
21+
}
22+
23+
privateTreeNoderemoveLeaves(TreeNoderoot) {
24+
if (root ==null) {
25+
returnnull;
26+
}
27+
28+
if (root.left ==null &&root.right ==null) {
29+
returnnull;
30+
}
31+
32+
root.left =removeLeaves(root.left);
33+
root.right =removeLeaves(root.right);
34+
35+
returnroot;
36+
}
37+
38+
privateList<Integer>getLeaves(TreeNoderoot) {
39+
Queue<TreeNode>queue =newLinkedList<>();
40+
queue.add(root);
41+
List<Integer>list =newArrayList<>();
42+
43+
while (!queue.isEmpty()) {
44+
TreeNodenode =queue.remove();
45+
if (node.left ==null &&node.right ==null) {
46+
list.add(node.val);
47+
}
48+
else {
49+
if (node.left !=null) {
50+
queue.add(node.left);
51+
}
52+
53+
if (node.right !=null) {
54+
queue.add(node.right);
55+
}
56+
}
57+
}
58+
59+
returnlist;
60+
}
61+
}

‎Medium/Inorder Successor in BST.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
classSolution {
11+
publicTreeNodeinorderSuccessor(TreeNoderoot,TreeNodep) {
12+
if (p.right !=null) {
13+
TreeNodenode =p.right;
14+
while (node.left !=null) {
15+
node =node.left;
16+
}
17+
18+
returnnode;
19+
}
20+
21+
returngetLastLeft(root,p);
22+
}
23+
24+
privateTreeNodegetLastLeft(TreeNoderoot,TreeNodep) {
25+
TreeNodelastLeft =null;
26+
27+
while (root !=null) {
28+
if (root ==p) {
29+
break;
30+
}
31+
elseif (root.val <p.val) {
32+
root =root.right;
33+
}
34+
else {
35+
lastLeft =root;
36+
root =root.left;
37+
}
38+
}
39+
40+
returnlastLeft;
41+
}
42+
}

‎Medium/Nested List Weight Sum II.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* public interface NestedInteger {
5+
* // Constructor initializes an empty nested list.
6+
* public NestedInteger();
7+
*
8+
* // Constructor initializes a single integer.
9+
* public NestedInteger(int value);
10+
*
11+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
12+
* public boolean isInteger();
13+
*
14+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
15+
* // Return null if this NestedInteger holds a nested list
16+
* public Integer getInteger();
17+
*
18+
* // Set this NestedInteger to hold a single integer.
19+
* public void setInteger(int value);
20+
*
21+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
22+
* public void add(NestedInteger ni);
23+
*
24+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
25+
* // Return null if this NestedInteger holds a single integer
26+
* public List<NestedInteger> getList();
27+
* }
28+
*/
29+
classSolution {
30+
intmaxDepth =1;
31+
intsum =0;
32+
publicintdepthSumInverse(List<NestedInteger>nestedList) {
33+
for (NestedIntegern :nestedList) {
34+
updateMaxDepth(n,1);
35+
}
36+
37+
for (NestedIntegern :nestedList) {
38+
dfsHelper(n,maxDepth);
39+
}
40+
41+
returnsum;
42+
}
43+
44+
privatevoiddfsHelper(NestedIntegern,intdepth) {
45+
if (n.isInteger()) {
46+
sum +=n.getInteger() *depth;
47+
}
48+
else {
49+
for (NestedIntegeri :n.getList()) {
50+
dfsHelper(i,depth-1);
51+
}
52+
}
53+
}
54+
55+
privatevoidupdateMaxDepth(NestedIntegern,intdepth) {
56+
if (n.isInteger()) {
57+
maxDepth =Math.max(maxDepth,depth);
58+
}
59+
else {
60+
for (NestedIntegeri :n.getList()) {
61+
updateMaxDepth(i,depth+1);
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp