File tree Expand file tree Collapse file tree 3 files changed +169
-3
lines changed
0900-0999/0938.Range Sum of BST
1300-1399/1304.Find N Unique Integers Sum up to Zero Expand file tree Collapse file tree 3 files changed +169
-3
lines changed Original file line number Diff line number Diff line change 4747<!-- 这里可写当前语言的特殊实现逻辑 -->
4848
4949``` python
50-
50+ class Solution :
51+ def rangeSumBST (self , root , L , R ):
52+ """
53+ :type root: TreeNode
54+ :type L: int
55+ :type R: int
56+ :rtype: int
57+ """
58+ def searchBST (node ):
59+ if not node:
60+ return
61+ if L <= node.val <= R:
62+ self .ans += node.val
63+ searchBST(node.right)
64+ searchBST(node.left)
65+ elif node.val < L:
66+ searchBST(node.right)
67+ elif node.val > R:
68+ searchBST(node.left)
69+ self .ans = 0
70+ searchBST(root)
71+ return self .ans
5172```
5273
5374### ** Java**
5475
5576<!-- 这里可写当前语言的特殊实现逻辑 -->
5677
5778``` java
79+ class Solution {
80+ private int res = 0 ;
81+ public int rangeSumBST (TreeNode root , int L , int R ) {
82+ if (root == null ) {
83+ return res;
84+ }
85+
86+ if (root. val < L ) {
87+ rangeSumBST(root. right, L , R );
88+ } else if (root. val > R ) {
89+ rangeSumBST(root. left, L , R );
90+ } else {
91+ res += root. val;
92+ rangeSumBST(root. left, L , R );
93+ rangeSumBST(root. right, L , R );
94+ }
95+ return res;
96+
97+ }
98+ }
99+ ```
58100
101+ ### ** C++**
102+
103+ ``` cpp
104+ class Solution {
105+ public:
106+ int rangeSumBST(TreeNode* root, int L, int R) {
107+ if (nullptr == root)
108+ return 0 ;
109+ stack<TreeNode * > s ;
110+ s.push(root) ;
111+ int sum = 0 ;
112+ while (!s.empty())
113+ {
114+ TreeNode * node = s.top() ;
115+ s.pop() ;
116+
117+ if (nullptr == node)
118+ continue ;
119+
120+ if (node->val > R)
121+ s.push(node->left) ;
122+ else if (node->val < L)
123+ s.push(node->right) ;
124+ else
125+ {
126+ sum += node->val ;
127+ s.push(node->left) ;
128+ s.push(node->right) ;
129+ }
130+ }
131+
132+ return sum ;
133+ }
134+ };
135+
136+ static int x = [ ] ( )
137+ {
138+ ios::sync_with_stdio (false);
139+ cin.tie(nullptr);
140+ return 0;
141+ }() ;
59142```
60143
61144### **...**
Original file line number Diff line number Diff line change 5858### ** Python3**
5959
6060``` python
61-
61+ class Solution :
62+ def rangeSumBST (self , root , L , R ):
63+ """
64+ :type root: TreeNode
65+ :type L: int
66+ :type R: int
67+ :rtype: int
68+ """
69+ def searchBST (node ):
70+ if not node:
71+ return
72+ if L <= node.val <= R:
73+ self .ans += node.val
74+ searchBST(node.right)
75+ searchBST(node.left)
76+ elif node.val < L:
77+ searchBST(node.right)
78+ elif node.val > R:
79+ searchBST(node.left)
80+ self .ans = 0
81+ searchBST(root)
82+ return self .ans
6283```
6384
6485### ** Java**
6586
6687``` java
88+ class Solution {
89+ private int res = 0 ;
90+ public int rangeSumBST (TreeNode root , int L , int R ) {
91+ if (root == null ) {
92+ return res;
93+ }
94+
95+ if (root. val < L ) {
96+ rangeSumBST(root. right, L , R );
97+ } else if (root. val > R ) {
98+ rangeSumBST(root. left, L , R );
99+ } else {
100+ res += root. val;
101+ rangeSumBST(root. left, L , R );
102+ rangeSumBST(root. right, L , R );
103+ }
104+ return res;
105+
106+ }
107+ }
108+ ```
67109
110+ ### ** C++**
111+
112+ ``` cpp
113+ class Solution {
114+ public:
115+ int rangeSumBST(TreeNode* root, int L, int R) {
116+ if (nullptr == root)
117+ return 0 ;
118+ stack<TreeNode * > s ;
119+ s.push(root) ;
120+ int sum = 0 ;
121+ while (!s.empty())
122+ {
123+ TreeNode * node = s.top() ;
124+ s.pop() ;
125+
126+ if (nullptr == node)
127+ continue ;
128+
129+ if (node->val > R)
130+ s.push(node->left) ;
131+ else if (node->val < L)
132+ s.push(node->right) ;
133+ else
134+ {
135+ sum += node->val ;
136+ s.push(node->left) ;
137+ s.push(node->right) ;
138+ }
139+ }
140+
141+ return sum ;
142+ }
143+ };
144+
145+ static int x = [ ] ( )
146+ {
147+ ios::sync_with_stdio (false);
148+ cin.tie(nullptr);
149+ return 0;
150+ }() ;
68151```
69152
70153### **...**
Original file line number Diff line number Diff line change @@ -9,4 +9,4 @@ public int[] sumZero(int n) {
99 ret [n - 1 ] = -preSum ;
1010 return ret ;
1111 }
12- }
12+ }
You can’t perform that action at this time.
0 commit comments