Skip to content

Commit 737228f

Browse files
committed
feat: problem 08
1 parent 19d0862 commit 737228f

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

β€ŽDaily_Coding_Problem-07.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ class SolutionLeetcode {
7777
// converte os dois caracteres para inteiro e verifica se Γ© menor ou igual a 26, se for, soma o elemento atual com o
7878
// elemento seguinte e o elemento atual com o elemento seguinte + 1, senΓ£o, soma o elemento atual com o elemento seguinte, ex: 111 = 3 (aaa, ka, ak)
7979
}
80-
return dp[0]; // retorna o primeiro elemento do vetor para saber a quantidade de combinaΓ§Γ΅es possiveis de decodificaΓ§Γ£o ex: 111 = 3 (aaa, ka, ak)
80+
return dp[0]; // retorna o primeiro elemento do vetor para saber a quantidade de combinaΓ§Γ΅es possiveis de decodificaΓ§Γ£o ex: 111 = 3 (aaa, ka, ak)
8181
}
8282
};

β€ŽDaily_Coding_Problem-08.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "Daily_Coding_Problem.h"
2+
3+
/* MARK: - SOLUTION ✨ */
4+
5+
// Cria uma Tree Node (https://www.geeksforgeeks.org/binary-tree-set-1-introduction/)
6+
struct TreeNode {
7+
int val;
8+
TreeNode *left;
9+
TreeNode *right;
10+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11+
};
12+
13+
class Solution {
14+
public:
15+
int count = 0; // contador de possibilidades
16+
bool isUnivalTree(TreeNode* root) { // funΓ§Γ£o que verifica se a Γ‘rvore Γ© unival
17+
if (root == NULL) return true; // se a raiz for nula, retorna true
18+
if (root->left == NULL && root->right == NULL) { // se a raiz nΓ£o for nula, mas os filhos forem nulos, retorna true
19+
count++; // incrementa o contador
20+
return true; // retorna true se a raiz nΓ£o for nula e os filhos forem nulos
21+
}
22+
bool left = isUnivalTree(root->left); // chama a funΓ§Γ£o isUnivalTree para o filho da esquerda
23+
bool right = isUnivalTree(root->right); // chama a funΓ§Γ£o isUnivalTree para o filho da direita
24+
if (left && right) { // se os filhos forem unival
25+
if (root->left != NULL && root->val != root->left->val) return false; // se o filho da esquerda nΓ£o for nulo e o valor da raiz for diferente do valor do filho da esquerda, retorna false
26+
if (root->right != NULL && root->val != root->right->val) return false; // se o filho da direita nΓ£o for nulo e o valor da raiz for diferente do valor do filho da direita, retorna false
27+
count++; // incrementa o contador
28+
return true; // retorna true se os filhos forem unival
29+
}
30+
return false; // retorna false se os filhos nΓ£o forem unival
31+
}
32+
private:
33+
bool isUnivalTree(TreeNode* root, int val) {
34+
if (root == NULL) return true;
35+
if (root->val == val) { // se o valor da raiz for igual ao valor passado
36+
return isUnivalTree(root->left, val) && isUnivalTree(root->right, val); // se o valor da raiz for igual ao valor passado, chama a funΓ§Γ£o isUnivalTree para o filho da esquerda e para o filho da direita
37+
}
38+
return false;
39+
}
40+
};
41+
42+
int main()
43+
{
44+
Solution solution;
45+
TreeNode* root = new TreeNode(0);
46+
root->left = new TreeNode(1);
47+
root->right = new TreeNode(0);
48+
root->right->left = new TreeNode(1);
49+
root->right->right = new TreeNode(0);
50+
root->right->left->left = new TreeNode(1);
51+
root->right->left->right = new TreeNode(1);
52+
solution.isUnivalTree(root);
53+
std::cout << "Number of unival subtrees: " << solution.count << std::endl;
54+
}

β€ŽReadme.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ For example, given `[10, 15, 3, 7]` and `k` of `17`, return true since `10 + 7`
1414

1515
Bonus: Can you do this in one pass?
1616

17-
[Solution πŸŽ‰](https://github.com/All3yp/Daily-Coding-problems/blob/main/Daily_Coding_Problem-01.cpp)
17+
[Solution πŸŽ‰](Daily_Coding_Problem-01.cpp)
1818
#### Click [__*here*__](https://leetcode.com/problems/two-sum/) to visit this question on [*LeetCode*](https://leetcode.com/).
1919
---
2020

@@ -32,7 +32,7 @@ the expected output would be `[2, 3, 6]`.
3232

3333
Follow-up: what if you can't use division?
3434

35-
[Solution πŸŽ‰](https://github.com/All3yp/Daily-Coding-problems/blob/main/Daily_Coding_Problem-02.cpp)
35+
[Solution πŸŽ‰](Daily_Coding_Problem-02.cpp)
3636
#### Click [__*here*__](https://leetcode.com/problems/product-of-array-except-self/) to visit this question on [*LeetCode*](https://leetcode.com/).
3737
---
3838

@@ -64,7 +64,7 @@ const node = new TreeNode(
6464
expect(deserialize(serialize(node)).left.left.val).toEqual('left.left');
6565
```
6666

67-
[Solution πŸŽ‰](https://github.com/All3yp/Daily-Coding-problems/blob/main/Daily_Coding_Problem-03.cpp)
67+
[Solution πŸŽ‰](Daily_Coding_Problem-03.cpp)
6868
#### Click [__*here*__](https://leetcode.com/problems/serialize-and-deserialize-bst/) to visit this question on [*LeetCode*](https://leetcode.com/).
6969
---
7070

@@ -78,7 +78,7 @@ For example, the input `[3, 4, -1, 1]` should give 2. The input `[1, 2, 0]` shou
7878

7979
You can modify the input array in-place.
8080

81-
[Solution πŸŽ‰](https://github.com/All3yp/Daily-Coding-problems/blob/main/Daily_Coding_Problem-04.cpp)
81+
[Solution πŸŽ‰](Daily_Coding_Problem-04.cpp)
8282
#### Click [__*here*__](https://leetcode.com/problems/first-missing-positive/) to visit a similar question on [*LeetCode*](https://leetcode.com/).
8383
---
8484

@@ -101,7 +101,7 @@ function cons(a, b) {
101101

102102
Implement car and cdr.
103103

104-
[πŸŽ‰ Solution C++](https://github.com/All3yp/Daily-Coding-problems/blob/main/Daily_Coding_Problem-05.cpp)</br>
104+
[πŸŽ‰ Solution C++](Daily_Coding_Problem-05.cpp)</br>
105105
[πŸŽ‰ Solution Swift](https://github.com/All3yp/Daily-Coding-problems/blob/main/Daily_Coding_problem-05.swift)
106106
#### Click [__*here*__](https://leetcode.com/problems/number-of-good-pairs/) to visit a similar question on [*LeetCode*](https://leetcode.com/).
107107

@@ -115,7 +115,7 @@ An XOR linked list is a more memory efficient doubly linked list. Instead of eac
115115

116116
If using a language that has no pointers (such as Python), you can assume you have access to `get_pointer` and `dereference_pointer` functions that converts between nodes and memory addresses.
117117

118-
[Solution πŸŽ‰](https://github.com/All3yp/Daily-Coding-problems/blob/main/Daily_Coding_Problem-06.cpp)
118+
[Solution πŸŽ‰](Daily_Coding_Problem-06.cpp)
119119
#### Click [__*here*__](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) to visit a similar question on [*LeetCode*](https://leetcode.com/).
120120

121121
---
@@ -128,7 +128,31 @@ Given the mapping `a = 1, b = 2, ... z = 26`, and an encoded message, count the
128128
For example, the message `'111'` would give 3, since it could be decoded as `'aaa'`, `'ka'`, and `'ak'`.
129129
You can assume that the messages are decodable. For example, `'001'` is not allowed.
130130

131-
[Solution πŸŽ‰](https://github.com/All3yp/Daily-Coding-problems/blob/main/Daily_Coding_Problem-07.cpp)
131+
[Solution πŸŽ‰](Daily_Coding_Problem-07.cpp)
132132
#### Click [__*here*__](https://leetcode.com/problems/decode-ways/) to visit this question on [*LeetCode*](https://leetcode.com/).
133133

134134
---
135+
136+
## πŸš€ Problem 8 [Easy]
137+
138+
#### This problem was asked by Google.
139+
140+
A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.
141+
Given the root to a binary tree, count the number of unival subtrees.
142+
For example, the following tree has 5 unival subtrees:
143+
144+
```
145+
0
146+
/ \
147+
1 0
148+
/ \
149+
1 0
150+
/ \
151+
1 1
152+
```
153+
154+
[Solution πŸŽ‰](Daily_Coding_Problem-08.cpp)
155+
156+
<!-- #### Click [__*here*__]() to visit this question on [*LeetCode*](https://leetcode.com/). -->
157+
158+
---

0 commit comments

Comments
Β (0)