We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5198ec5 commit e64d252Copy full SHA for e64d252
solution/0437. Path Sum III/Solution.java
@@ -0,0 +1,35 @@
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
+
12
+ public int pathSum(TreeNode root, int sum) {
13
+ if (root == null) return 0;
14
+ int res = 0;
15
+ LinkedList<TreeNode> queue = new LinkedList<>();
16
+ queue.offer(root);
17
+ while (!queue.isEmpty()) {
18
+ TreeNode node = queue.poll();
19
+ res += solution(node, sum);
20
+ if (node.left != null) {
21
+ queue.offer(node.left);
22
+ }
23
+ if (node.right != null) {
24
+ queue.offer(node.right);
25
26
27
+ return res;
28
29
30
+ private int solution(TreeNode root, int sum) {
31
32
+ int res = sum == root.val ? 1 :
33
+ return solution(root.left, sum - root.val) + solution(root.right, sum - root.val) + res;
34
35
+}
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments