- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的不理解计算二叉树高度的代码背后的逻辑。如果有人看懂了,能简单解释一下吗?
我尝试通过放置断点来理解,但我仍然不清楚逻辑。
import pdb
class Node:
def __init__(self,data):
self.right=self.left=None
self.data = data
#print(self.data)
class Solution: #creating and inserting the nodes
def insert(self,root,data):
#print(data)
if root==None:
return Node(data)
else:
if data<=root.data:
cur=self.insert(root.left,data)
root.left=cur
else:
cur=self.insert(root.right,data)
root.right=cur
return root
def getHeight(self,root): #finding the height of the largest
branchintree
#Write your code here
if not root:
return -1
if not root.left and not root.right:
return 0
left_height=self.getHeight(root.left)
#print('left_height',left_height)
right_height=self.getHeight(root.right)
#print('right_height',right_height)
r=max(left_height,right_height) + 1
#print('r',r)
return max(left_height,right_height) + 1
T=int(input())
pdb.set_trace()
myTree=Solution()
root=None
for i in range(T):
data=int(input())
root=myTree.insert(root,data)
height=myTree.getHeight(root)
print(height)
最佳答案
请注意,查找树的高度的算法对于二叉搜索树和一般二叉树是相同的,因此出于本次讨论的目的,我将忽略 BST。
这段代码不是特别清楚,不怪你看不懂。
这里重写一下以消除噪音:
from collections import namedtuple
Node = namedtuple("Node", "data left right")
def height(root):
return max(height(root.left), height(root.right)) + 1 if root else 0
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^
# | | |
# | | base case; root is None
# | add the current node's height
# recursively find the maximum height of the current node's children
if __name__ == "__main__":
""" 1
/ \
2 3
\ /
4 5
\
6
"""
root = Node(
1,
Node(
2,
None,
Node(4, None, None)
),
Node(
3,
Node(
5,
None,
Node(6, None, None)
),
None
)
)
print(height(root)) # => 4
现在,我们在给定的 height
调用中有两种情况:
root
是 None
,在这种情况下我们返回 0,因为我们什么都没有。这是我们的基本情况,或终止递归的条件。root
不是 None
,在这种情况下,我们返回 1 以计算此特定递归调用考虑的当前节点的高度 plus 以当前节点为根的左右子树高度的最大值。这是关键的递归步骤。让我们来看看对上面示例树的 height
的调用。
我们首先访问节点 {1}
作为我们的根。这不是基本情况,因此 {1}
向其左子 {2}
询问其最大高度。 {2}
也不是基本情况,因此它向其左 child None
询问其总数。 None
是我们的第一个基本情况,将 0 返回给 {2}
。 {2}
然后询问其右 child {4}
的高度。 {4}
是一个带有两个返回 0 的空 None
引用的叶子,但是 {4}
为自己添加 1 并将其返回给 {2}
。
节点 {2}
现在可以计算 max(height(root.left), height(root.right))
,即 max(0, 1 ) => 1
,并且 {2}
可以加 1 来计算自己的高度,并向 {1}
报告其总数为 2。
我们的根 {1}
现在左子树的高度为 2,但是它还没有检查它的右子树,所以 max(height(root.left), height(root.right))
必须等待。
{1}
的右子树的过程基本相同:如果节点不是叶节点,它会询问其子节点各自的高度并取最大值,加 1计算自己,并向父级报告。在这种情况下,{6}
向 {5}
报告高度 1,{5}
向 报告高度 2 {3}
和 {3}
向 {1}
报告高度 3。最后,{1}
可以计算 max(2, 3)
并为其自身的高度加 1,将最终结果 4 返回给调用者。
如果所有这些都有些抽象,您始终可以使用递归调用工具来添加 depth
参数并打印其状态。
from collections import namedtuple
Node = namedtuple("Node", "data left right")
def height(root, depth=0):
if root:
print(" " * depth + f"{{{root.data}}} asking children...")
left_height = height(root.left, depth + 4)
right_height = height(root.right, depth + 4)
ret = max(left_height, right_height) + 1
print(" " * depth + f"{{{root.data}}} reporting max({left_height}, {right_height}) + 1 = {ret}")
return ret
print(" " * depth + "None returning 0")
return 0
if __name__ == "__main__":
""" 1
/ \
2 3
\ /
4 5
\
6
"""
root = Node(
1,
Node(
2,
None,
Node(4, None, None)
),
Node(
3,
Node(
5,
None,
Node(6, None, None)
),
None
)
)
print(height(root)) # => 4
输出:
{1} asking children...
{2} asking children...
None returning 0
{4} asking children...
None returning 0
None returning 0
{4} reporting max(0, 0) + 1 = 1
{2} reporting max(0, 1) + 1 = 2
{3} asking children...
{5} asking children...
None returning 0
{6} asking children...
None returning 0
None returning 0
{6} reporting max(0, 0) + 1 = 1
{5} reporting max(0, 1) + 1 = 2
None returning 0
{3} reporting max(2, 0) + 1 = 3
{1} reporting max(2, 3) + 1 = 4
4
关于python-3.x - getHeight是如何递归确定二叉树的高度的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58761439/
我有下面的图表,它填充了显示器的宽度和高度。高度始终只比屏幕大一点,因此会出现滚动条以显示底部 20 像素左右。 有没有办法让 Kendo UI 显示 100%,而不是 105% 的高度? 在线示例:
这个问题在这里已经有了答案: Why doesn't height: 100% work to expand divs to the screen height? (12 个答案) 关闭 9 年前
此页面 ( http://purcraft.com/madeinla/) 有问题,我正在尝试使用 iframe 元素显示此页面的内容:( http://purcraft.com/madeinla/ho
我在一个父 div 中有 2 个子 div。 Child1 是标题,Child2 是正文。我希望将 Child 2 的高度设置为 Parent - Child1 的高度。 Child2 有内容,所以它
我正在尝试用图像填充窗口。我正在使用 CSS 来尝试解决这个问题,但我想知道是否有一种方法可以最大化图像的宽度/高度,直到所有空白区域都被填满,但又不会破坏质量。 .rel-img-co
这个问题在这里已经有了答案: How to make a div 100% height of the browser window (41 个回答) 关闭 8 年前。
这可能是一个新手问题,但是是否可以将 Sprite 图标添加到带有文本的标签中? 例如: labeltext .icon { width: 30px height: 30px;
我有 3 个 div,分别是 header、content 和 footer。页眉和页脚具有固定的高度,并且它们被设计为 float 在顶部和底部。我想要使用 jquery 自动计算中间的 con
我有一个外部 div,其指定的宽度/高度(以毫米为单位)。 (mm只是赋值,不用于渲染)。 里面有另一个 div,其实际宽度/高度(以 px 为单位)。 两个 div 可以具有不同的比例。 我想要做的
我正在为一个非常简单的画廊 webapp 进行布局排序,但是当我使用 HTML5 文档类型声明时,我的一些 div(100%)的高度会立即缩小,我不能似乎使用 CSS 将它们丰满起来。 我的 HTML
我正在为一个非常简单的画廊 webapp 进行布局排序,但是当我使用 HTML5 文档类型声明时,我的一些 div(100%)的高度会立即缩小,我不能似乎使用 CSS 将它们丰满起来。 我的 HTML
我想更改 UISearchBar。文本字段的高度和宽度。我的问题是如何更改 iphone 中 UISearchBar 中的 UiSearchbar 高度、宽度、颜色 和 Uitextfield 高度?
我想要两个宽度和高度均为 100% 的 div。我知道子 div 不会工作,因为父 div 没有特定的高度,但有没有办法解决这个问题? HTML: CSS: body
我有几个带有“priceText”类的 div,我试图实现如果 div.priceText 高度小于 100px,则隐藏 this div 中的图像。 我无法让它工作。我已成功隐藏所有 .priceT
我正在尝试从 Image 列中列出的图像中获取实际图像尺寸,并将其显示在 Image Size 列中。 我遇到的问题是,我只能获取第一张图片的大小,该图片会添加到 Image Size 列的每个单元格
我正在使用一个插件,它要求我在加载图像后获取图像的宽度和高度,而不管图像的尺寸是如何确定的。
我有一个示例 pdf(已附),它包括一个文本对象和一个高度几乎相同的矩形对象。然后我使用 itextrup 检查了 pdf 的内容,如下所示: 1 1 1 RG 1 1 1 rg 0.12 0 0 0
我是 WPF 新手。我试图解决的一个问题是如何在运行时获得正确的高度。 在我的应用程序中,我将用户控件动态添加到代码隐藏中的 Stackpanel。 Usercontrol 包含一些 Texblock
在自定义 WPF 控件中,我想将控件的宽度设置为高度的函数。例如:Width = Height/3 * x; 实现此目的的最佳方法是什么,以便控件正确且流畅地调整大小(和初始大小)? 最佳答案 您可以
好吧,我本以为这是一个简单的问题,但显然它让我感到困惑。 当我尝试设置 RibbonComboBox 的高度时,它不会移动它的实际大小,而是移动它周围的框。 这是我的 XAML:
我是一名优秀的程序员,十分优秀!