Skip to content

Commit 8b03796

Browse files
committed
add 0938 cpp version(40ms)
1 parent f17d697 commit 8b03796

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed
+46-29
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
1-
# Definition for a binary tree node.
2-
# class TreeNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.left = None
6-
# self.right = None
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
int rangeSumBST(TreeNode* root, int L, int R) {
13+
if (nullptr == root)
14+
return 0 ;
15+
stack<TreeNode *> s ;
16+
s.push(root) ;
17+
int sum = 0 ;
18+
while (!s.empty())
19+
{
20+
TreeNode *node = s.top() ;
21+
s.pop() ;
22+
23+
if (nullptr == node)
24+
continue ;
25+
26+
if (node->val > R)
27+
s.push(node->left) ;
28+
else if (node->val < L)
29+
s.push(node->right) ;
30+
else
31+
{
32+
sum += node->val ;
33+
s.push(node->left) ;
34+
s.push(node->right) ;
35+
}
36+
}
37+
38+
return sum ;
39+
}
40+
};
741

8-
9-
class Solution:
10-
def rangeSumBST(self, root, L, R):
11-
"""
12-
:type root: TreeNode
13-
:type L: int
14-
:type R: int
15-
:rtype: int
16-
"""
17-
def searchBST(node):
18-
if not node:
19-
return
20-
if L <= node.val <= R:
21-
self.ans += node.val
22-
searchBST(node.right)
23-
searchBST(node.left)
24-
elif node.val < L:
25-
searchBST(node.right)
26-
elif node.val > R:
27-
searchBST(node.left)
28-
self.ans = 0
29-
searchBST(root)
30-
return self.ans
42+
static int x = []()
43+
{
44+
ios::sync_with_stdio(false);
45+
cin.tie(nullptr);
46+
return 0;
47+
}() ;

solution/0953.Verifying an Alien Dictionary/Solution.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
bool cmp(string a, string b)
2222
{
23-
int i ;
24-
int len = min(a.size(), b.size()) ;
23+
int i, len = min(a.size(), b.size()) ;
2524
for (i = 0; i < len; ++i)
2625
{
2726
int c = ala[ a[i] - 'a' ] - ala[ b[i] - 'a' ] ;
@@ -31,9 +30,6 @@
3130
return false ;
3231
}
3332

34-
if (i == a.size())
35-
return true ;
36-
else
37-
return false ;
33+
return i == a.size() ;
3834
}
3935
};

0 commit comments

Comments
 (0)