Skip to content

Commit be89904

Browse files
authored
feat: add problem minimum-flips-in-binary-tree-to-get-result with unit tests (#77)
closes #76
1 parent a835dd9 commit be89904

12 files changed

Lines changed: 341 additions & 18 deletions

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,20 @@ This repository covers a comprehensive range of algorithmic patterns and data st
164164
- [Two Pointers](#two-pointers)
165165
- [Sliding Window](#sliding-window)
166166
- [Stack](#stack)
167-
- [Matrix](#matrix)
168-
- [Intervals](#intervals)
167+
- [Binary Search](#binary-search)
169168
- [Linked List](#linked-list)
170-
- [Binary Tree General](#binary-tree-general)
171-
- [Binary Tree BFS](#binary-tree-bfs)
172-
- [Binary Search Tree](#binary-search-tree)
173-
- [Graph General](#graph-general)
174-
- [Dynamic Programming](#dynamic-programming)
169+
- [Trees](#trees)
170+
- [Heap / Priority Queue](#heap-/-priority-queue)
175171
- [Backtracking](#backtracking)
176-
- [Heap](#heap)
172+
- [Tries](#tries)
173+
- [Graphs](#graphs)
174+
- [Advanced Graphs](#advanced-graphs)
175+
- [1-D Dynamic Programming](#1-d-dynamic-programming)
176+
- [2-D Dynamic Programming](#2-d-dynamic-programming)
177177
- [Greedy](#greedy)
178-
- [Trie](#trie)
178+
- [Intervals](#intervals)
179+
- [Math & Geometry](#math--geometry)
180+
- [Bit Manipulation](#bit-manipulation)
179181

180182
## Arrays & Hashing
181183

@@ -189,3 +191,9 @@ This repository covers a comprehensive range of algorithmic patterns and data st
189191
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
190192
|---|-------|----------|------|-------|------------|-----|------|
191193
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Python](./problems/valid_palindrome/valid_palindrome.py), [C++](./problems/valid_palindrome/valid_palindrome.cc) | _O(n)_ | _O(1)_ | Easy | | |
194+
195+
## Trees
196+
197+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
198+
|---|-------|----------|------|-------|------------|-----|------|
199+
| 2313 | [Minimum Flips in Binary Tree to Get Result](https://leetcode.com/problems/minimum-flips-in-binary-tree-to-get-result/description/) | [Python](./problems/minimum_flips_in_binary_tree_to_get_result/minimum_flips_in_binary_tree_to_get_result.py), [C++](./problems/minimum_flips_in_binary_tree_to_get_result/minimum_flips_in_binary_tree_to_get_result.cc) | _O(n)_ | _O(1)_ | Hard | | _n_ is the number of nodes in the binary tree. |

common/.gitkeep

Whitespace-only changes.

common/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .trees.treenode import TreeNode
2+
3+
__all__ = ["TreeNode"]

common/trees/treenode.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef COMMON_TREES_TREENODE_H
2+
#define COMMON_TREES_TREENODE_H
3+
4+
// Definition for a binary tree node.
5+
struct TreeNode {
6+
int val;
7+
TreeNode *left;
8+
TreeNode *right;
9+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
10+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
11+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
12+
};
13+
14+
#endif // COMMON_TREES_TREENODE_H

common/trees/treenode.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Common tree node definition."""
2+
3+
# Definition for a binary tree node.
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right

config/tags.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ tags:
33
- "Two Pointers"
44
- "Sliding Window"
55
- "Stack"
6-
- "Matrix"
7-
- "Intervals"
6+
- "Binary Search"
87
- "Linked List"
9-
- "Binary Tree General"
10-
- "Binary Tree BFS"
11-
- "Binary Search Tree"
12-
- "Graph General"
13-
- "Dynamic Programming"
8+
- "Trees"
9+
- "Heap / Priority Queue"
1410
- "Backtracking"
15-
- "Heap"
11+
- "Tries"
12+
- "Graphs"
13+
- "Advanced Graphs"
14+
- "1-D Dynamic Programming"
15+
- "2-D Dynamic Programming"
1616
- "Greedy"
17-
- "Trie"
17+
- "Intervals"
18+
- "Math & Geometry"
19+
- "Bit Manipulation"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
problem:
2+
number: 2313
3+
title: "Minimum Flips in Binary Tree to Get Result"
4+
leetcode_url: "https://leetcode.com/problems/minimum-flips-in-binary-tree-to-get-result/description/"
5+
difficulty: "hard"
6+
tags: ["Trees"]
7+
8+
solutions:
9+
python: "problems/minimum_flips_in_binary_tree_to_get_result/minimum_flips_in_binary_tree_to_get_result.py"
10+
cpp: "problems/minimum_flips_in_binary_tree_to_get_result/minimum_flips_in_binary_tree_to_get_result.cc"
11+
12+
complexity:
13+
time: "O(n)"
14+
space: "O(1)"
15+
16+
notes: "_n_ is the number of nodes in the binary tree."
17+
readme_link: ""
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "minimum_flips_in_binary_tree_to_get_result.h"
2+
3+
#include <algorithm>
4+
#include <functional>
5+
#include <utility>
6+
#include "../../common/trees/treenode.h"
7+
8+
using namespace std;
9+
10+
int minimumFlips(TreeNode* root, bool result) {
11+
function<pair<int, int>(TreeNode*)> dfs = [&](TreeNode* node) -> pair<int, int> {
12+
if (!node) {
13+
return {1 << 30, 1 << 30}; // Impossible case
14+
}
15+
const int x = node->val;
16+
if (x >= 0 && x <= 1) {
17+
return {x == 0 ? 0 : 1, x == 1 ? 0 : 1};
18+
}
19+
20+
auto [left_false, left_true] = dfs(node->left);
21+
auto [right_false, right_true] = dfs(node->right);
22+
if (x == 2) { // OR
23+
return {left_false + right_false, min({left_true + right_true, left_true + right_false,
24+
left_false + right_true})};
25+
} else if (x == 3) { // AND
26+
return {
27+
min({left_false + right_false, left_true + right_false, left_false + right_true}),
28+
left_true + right_true};
29+
} else if (x == 4) { // XOR
30+
return {min({left_false + right_false, left_true + right_true}),
31+
min({left_true + right_false, left_false + right_true})};
32+
} else if (x == 5) { // NOT
33+
return {left_true, left_false};
34+
} else {
35+
return {1 << 30, 1 << 30}; // Invalid operation
36+
}
37+
};
38+
return result ? dfs(root).second : dfs(root).first;
39+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "../../common/trees/treenode.h"
2+
3+
int minimumFlips(TreeNode* root, bool result);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from common import TreeNode
2+
from math import inf
3+
from typing import Optional, Tuple
4+
5+
6+
def minimumFlips(root: Optional[TreeNode], result: bool) -> int:
7+
def dfs(node: Optional[TreeNode]) -> Tuple[int, int]:
8+
"""Returns a tuple (min_flips_to_false, min_flips_to_true)"""
9+
if node is None:
10+
return inf, inf
11+
12+
x = node.val
13+
if x in (0, 1):
14+
return (1, 0) if x == 1 else (0, 1)
15+
16+
left_false, left_true = dfs(node.left)
17+
right_false, right_true = dfs(node.right)
18+
if x == 2: # OR
19+
return (
20+
left_false + right_false,
21+
min(
22+
left_true + right_false,
23+
left_false + right_true,
24+
left_true + right_true,
25+
),
26+
)
27+
elif x == 3: # AND
28+
return (
29+
min(
30+
left_false + right_false,
31+
left_false + right_true,
32+
left_true + right_false,
33+
),
34+
left_true + right_true,
35+
)
36+
elif x == 4: # XOR
37+
return (
38+
min(left_false + right_false, left_true + right_true),
39+
min(left_false + right_true, left_true + right_false),
40+
)
41+
elif x == 5: # NOT
42+
return (left_true, left_false)
43+
else:
44+
raise ValueError(f"Unknown operation: {x}")
45+
46+
return dfs(root)[int(result)]

0 commit comments

Comments
 (0)