当前位置:网站首页>[Niuke brush questions day4] jz55 depth of binary tree
[Niuke brush questions day4] jz55 depth of binary tree
2022-07-05 08:43:00 【strawberry47】
subject :
Enter a binary tree , Find the depth of the tree . The nodes that pass from the root node to the leaf node ( Containing root 、 leaf ) A path to a tree , The length of the longest path is the depth of the tree , The depth of the root node is treated as 1 .
Ideas :
First encounter the problem of trees , A little confused , I don't understand his construction process
The first reaction is to use recursion , Because the end condition is easy to think of : Both left and right nodes are empty
but, I don't know how to move the root node ...
answer :
Looking at the answer , The idea of recursion is also used :
mentally .. Recursion is so hard o(╥﹏╥)o
class Solution:
def TreeDepth(self , pRoot: TreeNode) -> int:
# write code here
if not pRoot:
return 0
left = right =0
if pRoot.left:
left = self.TreeDepth(pRoot.left)
if pRoot.right:
right = self.TreeDepth(pRoot.right)
return max([left,right])+1
You can also use the idea of queues :
import queue
class Solution:
def maxDepth(self , root: TreeNode) -> int:
# Empty nodes have no depth
if not root:
return 0
# Subsequent nodes of the queue maintenance hierarchy
q= queue.Queue()
# Root in the team
q.put(root)
# Record the depth
res = 0
# Level traversal
while not q.empty():
# Record how many nodes there are in the current layer
n = q.qsize()
# Traverse this layer , Go on to the next floor
for i in range(n):
node = q.get()
# Add the left and right nodes of the next layer
if node.left:
q.put(node.left)
if node.right:
q.put(node.right)
# Depth plus 1
res += 1
return res
The idea of queue is easier to understand , Is to save every layer , See how many layers you can save , Plus one
边栏推荐
- [three tier architecture and JDBC summary]
- EA introduction notes
- golang 基础 —— golang 向 mysql 插入的时间数据和本地时间不一致
- Yolov4 target detection backbone
- Example 007: copy data from one list to another list.
- js异步错误处理
- Daily question - input a date and output the day of the year
- 696. Count binary substring
- [牛客网刷题 Day4] JZ35 复杂链表的复制
- Halcon: check of blob analysis_ Blister capsule detection
猜你喜欢
随机推荐
Five design details of linear regulator
Bit operation related operations
Arduino burning program and Arduino burning bootloader
猜谜语啦(9)
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
C语言标准函数scanf不安全的原因
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
[formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
U8g2 drawing
Affected tree (tree DP)
猜谜语啦(142)
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
Sword finger offer 09 Implementing queues with two stacks
js异步错误处理
Guess riddles (10)
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
319. Bulb switch
Is the security account given by Yixue school safe? Where can I open an account
Classification of plastic surgery: short in long long long
Example 009: pause output for one second






![[noi simulation] juice tree (tree DP)](/img/19/bc71e8dc3958e4cb87b31423a74617.png)
