File tree Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int arrangeCoins (int n) {
4
+ long start = 0 , end = n;
5
+ long k, curr;
6
+ while (start <= end) {
7
+ k = start + (end - start) / 2 ;
8
+ curr = k * (k + 1 ) / 2 ;
9
+ if (curr == n) return (int ) k;
10
+ if (n < curr) end = k - 1 ;
11
+ else start = k + 1 ;
12
+ }
13
+ return (int )end;
14
+ }
15
+ };
Original file line number Diff line number Diff line change
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
+ vector<vector<int >> result;
13
+
14
+ void BFS (TreeNode *root, int level){
15
+ if (root == NULL ) return ;
16
+
17
+ if (level == result.size ()) result.push_back (vector<int >());
18
+
19
+ result[level].push_back (root->val );
20
+
21
+ BFS (root->left , level + 1 );
22
+ BFS (root->right , level + 1 );
23
+
24
+ }
25
+ vector<vector<int >> levelOrderBottom (TreeNode* root) {
26
+
27
+ if (root == NULL ) return vector<vector<int >>();
28
+ // BFS(root, 0);
29
+ // return vector<vector<int>> (result.rbegin(), result.rend());
30
+
31
+ queue<TreeNode *> q;
32
+ q.push (root);
33
+
34
+ vector<int > clevel;
35
+ vector<vector<int >> alevel;
36
+
37
+ while (!q.empty ()){
38
+ int sz = q.size ();
39
+ while (sz--){
40
+ TreeNode *curr = q.front ();
41
+ q.pop ();
42
+ clevel.push_back (curr->val );
43
+ if (curr->left )
44
+ q.push (curr->left );
45
+ if (curr->right )
46
+ q.push (curr->right );
47
+ }
48
+ alevel.push_back (clevel);
49
+ clevel.clear ();
50
+ }
51
+ return vector<vector<int >> (alevel.rbegin (), alevel.rend ());
52
+ }
53
+ };
You can’t perform that action at this time.
0 commit comments