avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

2331Calculate the value of the Boolean binary tree One question daily

  2024-01-01        
字数统计: 170字   |   阅读时长: 1min

topic:

2023-02-06.png
2331Calculate the value of the Boolean binary tree

Thought:

For the currentroot,in the case ofleaf root(That is, the left node is empty),Then judge the current Boolean value(The leaf node must be Boolean instead of judging)。
Otherwise,Until traversing the leaf node,After seeing the left and right children of a sub -tree,Judge the current Valtype,Finally seekingValValue。
ifValNot the root node,butValChildren’s nodes as other trees return to the previous layer。

Code:

1
2
3
4
5
6
7
class Solution:
def evaluateTree(self, root: Optional[TreeNode]) -> bool:
if root.left is None:
return bool(root.val)
l = self.evaluateTree(root.left)
r = self.evaluateTree(root.right)
return l or r if root.val == 2 else l and r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func evaluateTree(root *TreeNode) bool {
if root.Left == nil {
return root.Val == 1
}
l, r := evaluateTree(root.Left), evaluateTree(root.Right)
if root.Val == 2 {
return l || r
}
return l && r
}
  • Python
  • answer
  • Binary tree

扫一扫,分享到微信

微信分享二维码
2319Determine whether the matrix is ​​one X matrix One question daily
2335. The shortest total time to be filled with a cup One question daily
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号