当前位置:网站首页>[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
边栏推荐
- How can fresh students write resumes to attract HR and interviewers
- MATLAB小技巧(28)模糊綜合評價
- Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
- JS asynchronous error handling
- Reasons for the insecurity of C language standard function scanf
- 多元线性回归(sklearn法)
- 猜谜语啦(2)
- 2022.7.4-----leetcode.1200
- C语言标准函数scanf不安全的原因
- Explore the authentication mechanism of StarUML
猜你喜欢
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
猜谜语啦(142)
Halcon blob analysis (ball.hdev)
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
Redis implements a high-performance full-text search engine -- redisearch
Agile project management of project management
深度学习模型与湿实验的结合,有望用于代谢通量分析
IT冷知识(更新ing~)
Classification of plastic surgery: short in long long long
Example 006: Fibonacci series
随机推荐
Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
[daily training -- Tencent selected 50] 557 Reverse word III in string
IT冷知识(更新ing~)
Search data in geo database
Go dependency injection -- Google open source library wire
每日一题——输入一个日期,输出它是该年的第几天
猜谜语啦(7)
C# LINQ源码分析之Count
暑假第一周
猜谜语啦(6)
2022.7.4-----leetcode. one thousand and two hundred
STM32 lights up the 1.8-inch screen under Arduino IDE
Arduino+a4988 control stepper motor
某公司文件服务器迁移方案
每日一题——替换空格
整形的分类:short in long longlong
Business modeling of software model | stakeholders
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
Example 006: Fibonacci series
[牛客网刷题 Day4] JZ32 从上往下打印二叉树