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

Commit7566e15

Browse files
path sum
1 parent4e9a62b commit7566e15

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

‎EASY/src/easy/PathSum.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,37 @@ public boolean hasPathSum(TreeNode root, int sum) {
2727

2828
publicstaticvoidmain(String...strings){
2929
PathSumtest =newPathSum();
30-
// TreeNode root = new TreeNode(1);
31-
// root.left = new TreeNode(2);
32-
// int sum =1;
30+
TreeNoderoot =newTreeNode(5);
31+
root.left =newTreeNode(4);
32+
intsum =5;
3333

34-
TreeNoderoot =newTreeNode(1);
35-
root.left =newTreeNode(-2);
36-
root.left.left =newTreeNode(1);
37-
root.left.right =newTreeNode(3);
38-
root.right =newTreeNode(-3);
39-
root.right.left =newTreeNode(-2);
40-
root.left.left.left =newTreeNode(-1);
41-
intsum =2;
34+
// TreeNode root = new TreeNode(1);
35+
// root.left = new TreeNode(-2);
36+
// root.left.left = new TreeNode(1);
37+
// root.left.right = new TreeNode(3);
38+
// root.right = new TreeNode(-3);
39+
// root.right.left = new TreeNode(-2);
40+
// root.left.left.left = new TreeNode(-1);
41+
// int sum = 2;
4242
// 1
4343
// / \
4444
// -2 -3
4545
// / \ /
4646
// 1 3 -2
4747
// /
4848
// -1
49-
System.out.println(test.hasPathSum(root,sum));
49+
// System.out.println(test.hasPathSum(root, sum));
50+
System.out.println(test.hasPathSumAgain(root,sum));
5051
}
52+
53+
54+
publicbooleanhasPathSumAgain(TreeNoderoot,intsum) {
55+
if(root ==null)returnfalse;
56+
if(root.left ==null &&root.right ==null){
57+
if(sum ==root.val)returntrue;
58+
elsereturnfalse;
59+
}
60+
returnhasPathSum(root.left,sum -root.val) ||hasPathSum(root.right,sum -root.val);
61+
}
62+
5163
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../../blob/master/EASY/src/easy/PascalsTriangle.java)| O(n^2)|O(1)| Easy|
4242
|117|[Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)|[Solution](../../blob/master/HARD/src/hard/PopulatingNextRightPointersinEachNodeII.java)| O(n)|O(1) | Hard| BFS
4343
|116|[Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)|[Solution](../../blob/master/MEDIUM/src/medium/PopulatingNextRightPointersinEachNode.java)| O(n)|O(1) | Medium| BFS
44+
|112|[Path Sum](https://leetcode.com/problems/path-sum/)|[Solution](../../blob/master/EASY/src/easy/PathSum.java)| O(n)|O(1) | Easy| DFS
4445
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|[Solution](../../blob/master/EASY/src/easy/MinimumDepthofBinaryTree.java)| O(n)|O(1)~O(h) | Easy| BFS, DFS
4546
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../../blob/master/MEDIUM/src/medium/DecodeWays.java)| O(n)|O(n) | Medium| DP
4647
|98|[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)|[Solution] | O(n) | O(1) | Medium | DFS/Recursion

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp