英文:
Python nested if vs and behviour
问题
我正在使用Python解决二叉树的底部视图问题。这段代码是有效的。
class Solution:
# 从左到右返回二叉树的顶部视图中可见的节点列表。
def bottomView(self, root):
dic = {}
result = []
self.helper(root, dic, 0, 0)
for i in sorted(dic.keys()):
result.append(dic[i][0])
return result
def helper(self, root, d, hd, level):
if root is None:
return
if hd in d:
if level >= d[hd][1]:
d[hd] = (root.data, level)
else:
d[hd] = (root.data, level)
self.helper(root.left, d, hd - 1, level + 1)
self.helper(root.right, d, hd + 1, level + 1)
现在,我对这段代码有疑问:
if hd in d:
if level >= d[hd][1]:
d[hd] = (root.data, level)
如果我用这段代码替换它:
if hd in d and level >= d[hd][1]:
d[hd] = (root.data, level)
它不起作用。根据我对嵌套if块功能的理解,只有在外部if块的条件评估为True时,内部if块的条件才会被评估,这与在and语句中发生的情况类似。但是,前一个代码块给我正确的输出,而第二个代码块只是不起作用。为什么会这样?
英文:
I was solving the bottom view of a binary tree question using Python. This code is working.
class Solution:
#Function to return a list of nodes visible from the top view
#from left to right in Binary Tree.
def bottomView(self,root):
dic = {}
result = []
self.helper(root, dic, 0, 0)
for i in sorted(dic.keys()):
result.append(dic[i][0])
return result
def helper(self, root, d, hd, level):
if root is None:
return
if hd in d:
if level >= d[hd][1]:
d[hd] = (root.data, level)
else:
d[hd] = (root.data, level)
self.helper(root.left, d, hd - 1,level + 1)
self.helper(root.right, d, hd + 1,level + 1)
Now, I have a doubt regarding this block of code:
if hd in d:
if level >= d[hd][1]:
d[hd] = (root.data, level)
If I replace it with this code:
if hd in d and level >= d[hd][1]:
d[hd] = (root.data, level)
It does not work. As far as I understand the functionality of nested if block, the inner loop condition will be evaluated only if the outer loop condition evaluates to True which is similar to what is happening in the and statement. But the previous block gives me the correct output while the second block of code just does not work. Why is it so?
Link to problem statement :- https://practice.geeksforgeeks.org/problems/bottom-view-of-binary-tree/1
答案1
得分: 3
问题出在下面的else
块!
if hd in d:
if level >= d[hd][1]:
d[hd] = (root.data, level)
else:
d[hd] = (root.data, level) # 只有当hd不存在时才执行!
与以下代码不同:
if hd in d and level >= d[hd][1]:
d[hd] = (root.data, level)
else:
d[hd] = (root.data, level) # 当hd的值满足条件时也会执行
在第一个示例中,当hd in d
条件不满足时会执行else
块。
在第二个示例中,它执行得更频繁!即当更强的条件hd in d and level >= d[hd][1]
不满足时。
仔细看第二个示例:if
和else
执行完全相同的代码。这是没有意义的!
实际上,如果你想要简化一些代码,你可以这样做:
if hd not in d or level >= d[hd][1]:
d[hd] = (root.data, level)
英文:
The problem is the else
Block underneath!
if hd in d:
if level >= d[hd][1]:
d[hd] = (root.data, level)
else:
d[hd] = (root.data, level) # only when hd not there yet!
is different from:
if hd in d and level >= d[hd][1]:
d[hd] = (root.data, level)
else:
d[hd] = (root.data, level) # also when value for hd meets condition
In the first, the else is executed when hd in d
is not met.
In the second, it is executed more often! Namely when the stronger hd in d and level >= d[hd][1]
is not met.
Take a close look at the second one: if and else execute the exact same code. That has to be pointless!
If, in fact, you wanted to save some lines here, you could do:
if hd not in d or level >= d[hd][1]:
d[hd] = (root.data, level)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论