|
58347 | 58347 | <ul class="md-nav__list"> |
58348 | 58348 |
|
58349 | 58349 | <li class="md-nav__item"> |
58350 | | - <a href="#solution-1" class="md-nav__link"> |
| 58350 | + <a href="#solution-1-tree-dp-case-analysis" class="md-nav__link"> |
58351 | 58351 | <span class="md-ellipsis"> |
58352 | | - Solution 1 |
| 58352 | + Solution 1: Tree DP + Case Analysis |
58353 | 58353 | </span> |
58354 | 58354 | </a> |
58355 | 58355 |
|
@@ -86579,7 +86579,19 @@ <h2 id="description">Description</h2> |
86579 | 86579 | <h2 id="solutions">Solutions</h2> |
86580 | 86580 | <!-- solution:start --> |
86581 | 86581 |
|
86582 | | -<h3 id="solution-1">Solution 1</h3> |
| 86582 | +<h3 id="solution-1-tree-dp-case-analysis">Solution 1: Tree DP + Case Analysis</h3> |
| 86583 | +<p>We define a function <span class="arithmatex">\(dfs(root)\)</span>, which returns an array of length 2. The first element represents the minimum number of flips needed to change the value of the <span class="arithmatex">\(root\)</span> node to <code>false</code>, and the second element represents the minimum number of flips needed to change the value of the <span class="arithmatex">\(root\)</span> node to <code>true</code>. The answer is <span class="arithmatex">\(dfs(root)[result]\)</span>.</p> |
| 86584 | +<p>The implementation of the function <span class="arithmatex">\(dfs(root)\)</span> is as follows:</p> |
| 86585 | +<p>If <span class="arithmatex">\(root\)</span> is null, return <span class="arithmatex">\([+\infty, +\infty]\)</span>.</p> |
| 86586 | +<p>Otherwise, let <span class="arithmatex">\(x\)</span> be the value of <span class="arithmatex">\(root\)</span>, <span class="arithmatex">\(l\)</span> be the return value of the left subtree, and <span class="arithmatex">\(r\)</span> be the return value of the right subtree. Then we discuss the following cases:</p> |
| 86587 | +<ul> |
| 86588 | +<li>If <span class="arithmatex">\(x \in \{0, 1\}\)</span>, return <span class="arithmatex">\([x, x \oplus 1]\)</span>.</li> |
| 86589 | +<li>If <span class="arithmatex">\(x = 2\)</span>, which means the boolean operator is <code>OR</code>, to make the value of <span class="arithmatex">\(root\)</span> <code>false</code>, we need to make both the left and right subtrees <code>false</code>. Therefore, the first element of the return value is <span class="arithmatex">\(l[0] + r[0]\)</span>. To make the value of <span class="arithmatex">\(root\)</span> <code>true</code>, we need at least one of the left or right subtrees to be <code>true</code>. Therefore, the second element of the return value is <span class="arithmatex">\(\min(l[0] + r[1], l[1] + r[0], l[1] + r[1])\)</span>.</li> |
| 86590 | +<li>If <span class="arithmatex">\(x = 3\)</span>, which means the boolean operator is <code>AND</code>, to make the value of <span class="arithmatex">\(root\)</span> <code>false</code>, we need at least one of the left or right subtrees to be <code>false</code>. Therefore, the first element of the return value is <span class="arithmatex">\(\min(l[0] + r[0], l[0] + r[1], l[1] + r[0])\)</span>. To make the value of <span class="arithmatex">\(root\)</span> <code>true</code>, we need both the left and right subtrees to be <code>true</code>. Therefore, the second element of the return value is <span class="arithmatex">\(l[1] + r[1]\)</span>.</li> |
| 86591 | +<li>If <span class="arithmatex">\(x = 4\)</span>, which means the boolean operator is <code>XOR</code>, to make the value of <span class="arithmatex">\(root\)</span> <code>false</code>, we need both the left and right subtrees to be either <code>false</code> or <code>true</code>. Therefore, the first element of the return value is <span class="arithmatex">\(\min(l[0] + r[0], l[1] + r[1])\)</span>. To make the value of <span class="arithmatex">\(root\)</span> <code>true</code>, we need the left and right subtrees to be different. Therefore, the second element of the return value is <span class="arithmatex">\(\min(l[0] + r[1], l[1] + r[0])\)</span>.</li> |
| 86592 | +<li>If <span class="arithmatex">\(x = 5\)</span>, which means the boolean operator is <code>NOT</code>, to make the value of <span class="arithmatex">\(root\)</span> <code>false</code>, we need at least one of the left or right subtrees to be <code>true</code>. Therefore, the first element of the return value is <span class="arithmatex">\(\min(l[1], r[1])\)</span>. To make the value of <span class="arithmatex">\(root\)</span> <code>true</code>, we need at least one of the left or right subtrees to be <code>false</code>. Therefore, the second element of the return value is <span class="arithmatex">\(\min(l[0], r[0])\)</span>.</li> |
| 86593 | +</ul> |
| 86594 | +<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(n)\)</span>. Where <span class="arithmatex">\(n\)</span> is the number of nodes in the binary tree.</p> |
86583 | 86595 | <div class="tabbed-set tabbed-alternate" data-tabs="1:5"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label></div> |
86584 | 86596 | <div class="tabbed-content"> |
86585 | 86597 | <div class="tabbed-block"> |
|
0 commit comments