当前位置:网站首页>【LeetCode】100. Same tree
【LeetCode】100. Same tree
2022-07-01 04:14:00 【onlywishes】



Their thinking :
The structure and values of the tree are the same , That's right. .
1) According to the binary tree, there are left and right parts , So recursively compare the values of each corresponding node in the left part , The values of each corresponding node in the right part are compared , It is correct that they are all equal
if not p and not q: # If it's all empty , It's the same thing
return True
elif not p or not q: # If one is empty FALSE
return False
elif p.val != q.val: # If the corresponding node values are not equal F
return False
else :
return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
# Cycle the corresponding nodes of the left and right parts 2) Since the structure is required to be the same , Preorder traversal of two numbers , Each node corresponds to , If the values are equal True,
Use a stack to store the nodes of two trees , Store two corresponding nodes each time , Make sure the structure is the same ,
Because the previous sequence traverses around the root , Compare the root node first , In the left part of the comparison , The left part is gone , Compare the right part of the loop
stack = [] # Storage node data
if not p and not q: # All is empty TRUE
return True
elif not p or not q: # An empty FALSE
return False
while p or q or stack: # When the traversed node and stack are not all empty
while p or q:
if not p or not q: # When one of the two nodes traversed is empty
return False
if p.val != q.val: # Two corresponding nodes exist but their corresponding values are different F
return False
stack.append(p) # The nodes have the same value , After adding to the stack, the preamble traverses the next ,
stack.append(q)
p = p.left
q = q.left
q = stack.pop() # Reach the far left rear , Traverse the right child loop of the last node
p = stack.pop()
p = p.right
q = q.right # When the last two elements are taken out, the stack is empty , And the two nodes are empty , end
return True边栏推荐
- 208. implement trie (prefix tree)
- 不同性能测试工具的并发模式
- Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
- Qt development experience tips 226-230
- Embedded System Development Notes 79: why should I get the IP address of the local network card
- 熊市下的Coinbase:亏损、裁员、股价暴跌
- 分账技术赋能农贸市场,重塑交易管理服务效能
- Quickly filter data such as clock in time and date: Excel filter to find whether a certain time point is within a certain time period
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
- 409. longest palindrome
猜你喜欢

Recommend the best product development process in the Internet industry!

盘点华为云GaussDB(for Redis)六大秒级能力

Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security

431. encode n-ary tree as binary tree DFS

Use selenium automated test tool to climb the enrollment score line and ranking of colleges and universities related to the college entrance examination

这可能是你进腾讯最后的机会了..

Quickly filter data such as clock in time and date: Excel filter to find whether a certain time point is within a certain time period

【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码

Tip of edge browser: enter+ctrl can automatically convert the address bar into a web address

嵌入式系统开发笔记79:为什么要获取本机网卡IP地址
随机推荐
Recommend the best product development process in the Internet industry!
283. move zero
【TA-霜狼_may-《百人計劃》】1.2.1 向量基礎
有效的 @SuppressWarnings 警告名称
283.移动零
程序员女友给我做了一个疲劳驾驶检测
[EI conference] the Third International Conference on nanomaterials and nanotechnology in 2022 (nanomt 2022)
Libevent Library Learning
[TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation
类和对象收尾
All in one 1086: Jiaogu conjecture
[JPCs publication] the Third International Conference on control theory and application in 2022 (icocta 2022)
It's settled! 2022 JD cloud summit of JD global technology Explorer conference see you in Beijing on July 13
小程序中自定义组件
318. Maximum word length product
[Master / slave] router election in DD message
Note de développement du système embarqué 80: application du concepteur Qt à la conception de l'interface principale
使用WinMTR软件简单分析跟踪检测网络路由情况
Spock单元测试框架介绍及在美团优选的实践___第一章
TASK04|数理统计