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-difference-between-node-and-ancestor].html
2022-03-29 12:43:11 +08:00

38 lines
1.3 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> 节点 <code>A</code> 和 <code>B</code> 之间的最大值 <code>V</code>,其中 <code>V = |A.val - B.val|</code>,且 <code>A</code> 是 <code>B</code> 的祖先。</p>
<p>(如果 A 的任何子节点之一为 B或者 A 的任何子节点是 B 的祖先,那么我们认为 A 是 B 的祖先)</p>
<p> </p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/11/09/tmp-tree.jpg" style="width: 400px; height: 390px;" /></p>
<pre>
<strong>输入:</strong>root = [8,3,10,1,6,null,14,null,null,4,7,13]
<strong>输出:</strong>7
<strong>解释: </strong>
我们有大量的节点与其祖先的差值,其中一些如下:
|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
在所有可能的差值中,最大值 7 由 |8 - 1| = 7 得出。
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/09/tmp-tree-1.jpg" style="width: 250px; height: 349px;" />
<pre>
<strong>输入:</strong>root = [1,null,2,null,0,3]
<strong>输出:</strong>3
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中的节点数在 <code>2</code> 到 <code>5000</code> 之间。</li>
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
</ul>