1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/二叉搜索子树的最大键值和 [maximum-sum-bst-in-binary-tree].html
2022-03-29 12:43:11 +08:00

63 lines
1.9 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一棵以 <code>root</code> 为根的 <strong>二叉树</strong> ,请你返回 <strong>任意</strong> 二叉搜索子树的最大键值和。</p>
<p>二叉搜索树的定义如下:</p>
<ul>
<li>任意节点的左子树中的键值都 <strong>小于</strong> 此节点的键值。</li>
<li>任意节点的右子树中的键值都 <strong>大于</strong> 此节点的键值。</li>
<li>任意节点的左子树和右子树都是二叉搜索树。</li>
</ul>
<p> </p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/03/07/sample_1_1709.png" style="height: 250px; width: 320px;" /></p>
<pre>
<strong>输入:</strong>root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
<strong>输出:</strong>20
<strong>解释:</strong>键值为 3 的子树是和最大的二叉搜索树。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/03/07/sample_2_1709.png" style="height: 180px; width: 134px;" /></p>
<pre>
<strong>输入:</strong>root = [4,3,null,1,2]
<strong>输出:</strong>2
<strong>解释:</strong>键值为 2 的单节点子树是和最大的二叉搜索树。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>root = [-4,-2,-5]
<strong>输出:</strong>0
<strong>解释:</strong>所有节点键值都为负数,和最大的二叉搜索树为空。
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>root = [2,1,3]
<strong>输出:</strong>6
</pre>
<p><strong>示例 5</strong></p>
<pre>
<strong>输入:</strong>root = [5,4,8,3,null,6,3]
<strong>输出:</strong>7
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>每棵树有 <code>1</code><code>40000</code> 个节点。</li>
<li>每个节点的键值在 <code>[-4 * 10^4 , 4 * 10^4]</code> 之间。</li>
</ul>