LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
2024-10-12 20:56:00
Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
SAME TREE
''' SAME TREE Created on Nov 13, 2014 @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com> ''' # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param p, a tree node # @param q, a tree node # @return a boolean def isSameTree(self, p, q): self.__init__() self.foreachNode(p) seqP=self.seq self.__init__() self.foreachNode(q) seqQ=self.seq return seqP==seqQ def __init__(self): self.seq=[] def foreachNode(self, node): if(node==None): return self.seq.append(node.val) self.foreachNode(node.left) self.foreachNode(node.right)
MAX DEPTH
''' MAX DEPTH Created on Nov 13, 2014 @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com> ''' class Solution: # @param root, a tree node # @return an integer def maxDepth(self, root): self.__init__() self.foreachNode(root) return self.max def __init__(self): self.depth=0 self.max=0 def foreachNode(self, node): if(node==None): return self.depth+=1 if(node.left==None and node.right==None): if(self.depth>self.max): self.max=self.depth self.foreachNode(node.left) self.foreachNode(node.right) self.depth-=1 if __name__ == '__main__': pass
最新文章
- FineReport:任意时刻只允许在一个客户端登陆账号的插件
- http与https的区别
- 正则表达式30分钟入门:http://deerchao.net/tutorials/regex/regex.htm#mission
- Matlab最短路径问题记录
- Linux中iptables设置详细(转)
- oracle ORA-00001:违反唯一约束条件
- [读行者][学习LinqExpression和Reflection(Emit)]阅读TypeBuilderSample之ExampleFromTheArticle
- border粗细不一
- 云服务和虚拟机的预留 IP 地址
- C# 读取Excel日期格式
- oracle数据库连接无响应的解决
- Python内置函数str()和repr()
- Eclipse中GIT插件更新工程到之前版本
- 用Zephir编写PHP扩展
- 让数字变化炫酷起来,数字滚动Text组件[Unity]
- C#中dll调用方法
- Vue笔记:生命周期和钩子函数
- xunit-ICollectionFixture
- buildroot构建项目(三)--- u-boot 2017.11 适配开发板修改 1
- PAT甲级1139 First Contact