代码随想录|二叉树part03|
python
一、110.平衡二叉树
平衡二叉树的定义:左右子树深度相差不超过一
leetcode计算方法:根据节点计算
110.平衡二叉树
这题明明leetcode说比较的是深度,但是代码随想录比较的是高度,我就先学习比较高度吧
1.核心代码
代码如下(示例):
class Solution:
def isBalanced(self, root):
return self.gethight(root)!=-1
def gethight(self,root):
if not root:
return 0
left=self.gethight(root.left)
right=self.gethight(root.right)
if left==-1 or right==-1 or abs(left-right)>1:
return -1
else:
return 1+max(left,right)
二叉树还是没有完全掌握额
2.输入输出
import ast
class TreeNode:
def __init__(self,val,left=None,right=None):
self.val=val
self.left=left
self.right=right
class Solution:
def isBalanced(self, root):
return self.gethight(root)!=-1
def gethight(self,root):
if not root:
return 0
left=self.gethight(root.left)
right=self.gethight(root.right)
if left==-1 or right==-1 or abs(left-right)>1:
return -1
else:
return 1+max(left,right)
def buildtree(inroot):
if not inroot:
return None
root=TreeNode(int(inroot[0]))
queue=[root]
idx=1
while queue and idx<len(inroot):
cur=queue.pop(0) #pop第一个元素!
if idx<len(inroot) and inroot[idx]!="null":
cur.left=TreeNode(int(inroot[idx]))
queue.append(cur.left)
idx+=1
if idx<len(inroot) and inroot[idx]!="null":
cur.right=TreeNode(int(inroot[idx]))
queue.append(cur.right)
idx+=1
return root
if __name__=="__main__":
inroot=input().strip('[]').split(",")
root=buildtree(inroot)
solution=Solution()
result=solution.isBalanced(root)
print(result)
输入输出还是没记住怎么写的…
二、257.二叉树的所有路径
import ast
class TreeNode:
def __init__(self,val,left=None,right=None):
self.val=val
self.left=left
self.right=right
class Solution:
def binaryTreePaths(self, root):
stack=[root]
pathstack=[str(root.val)]
result=[]
while stack:
cur=stack.pop()
path=pathstack.pop()
if not (cur.left or cur.right):
result.append(path)
if cur.left:
stack.append(cur.left)
pathstack.append(path+'->'+str(cur.left.val))
if cur.right:
stack.append(cur.right)
pathstack.append(path+'->'+str(cur.right.val))
return result
def buildtree(inroot):
if not inroot:
return None
root=TreeNode(int(inroot[0]))
queue=[root]
idx=1
while queue and idx<len(inroot):
cur=queue.pop(0) #pop第一个元素!
if idx<len(inroot) and inroot[idx]!="null":
cur.left=TreeNode(int(inroot[idx]))
queue.append(cur.left)
idx+=1
if idx<len(inroot) and inroot[idx]!="null":
cur.right=TreeNode(int(inroot[idx]))
queue.append(cur.right)
idx+=1
return root
if __name__=="__main__":
inroot=input().strip('[]').split(",")
root=buildtree(inroot)
solution=Solution()
result=solution.binaryTreePaths(root)
print(result)
关键是要分别存节点和路径,然后要指针指着节点和路径
root是二叉树,但栈里面只能存列表,所以要加[root]
三、404.左叶子之和
404.左叶子之和
注意:左叶子,是指节点的左孩子,并且左孩子的左右节点都为0
右边和左边都会碰到左叶子的情况
import ast
class TreeNode:
def __init__(self,val,left=None,right=None):
self.val=val
self.left=left
self.right=right
class Solution:
def sumOfLeftLeaves(self, root):
if not root:
return 0
if not (root.left or root.right):
return 0
left=self.sumOfLeftLeaves(root.left)
if root.left and not (root.left.left or root.left.right):
left=root.left.val
right=self.sumOfLeftLeaves(root.right)
return left+right
def buildtree(inroot):
if not inroot:
return None
root=TreeNode(int(inroot[0]))
queue=[root]
idx=1
while queue and idx<len(inroot):
cur=queue.pop(0) #pop第一个元素!
if idx<len(inroot) and inroot[idx]!="null":
cur.left=TreeNode(int(inroot[idx]))
queue.append(cur.left)
idx+=1
if idx<len(inroot) and inroot[idx]!="null":
cur.right=TreeNode(int(inroot[idx]))
queue.append(cur.right)
idx+=1
return root
if __name__=="__main__":
inroot=input().strip('[]').split(",")
root=buildtree(inroot)
solution=Solution()
result=solution.sumOfLeftLeaves(root)
print(result)
完全从递归来做(嵌套自己的本身)
四、222.完全二叉树的节点
import ast
class TreeNode:
def __init__(self,val,left=None,right=None):
self.val=val
self.left=left
self.right=right
class Solution:
def countNodes(self, root):
if not root:
return 0
return 1+self.countNodes(root.left)+self.countNodes(root.right)
def buildtree(inroot):
if not inroot:
return None
root=TreeNode(int(inroot[0]))
queue=[root]
idx=1
while queue and idx<len(inroot):
cur=queue.pop(0) #pop第一个元素!
if idx<len(inroot) and inroot[idx]!="null":
cur.left=TreeNode(int(inroot[idx]))
queue.append(cur.left)
idx+=1
if idx<len(inroot) and inroot[idx]!="null":
cur.right=TreeNode(int(inroot[idx]))
queue.append(cur.right)
idx+=1
return root
if __name__=="__main__":
inroot=input().strip('[]').split(",")
root=buildtree(inroot)
solution=Solution()
result=solution.countNodes(root)
print(result)
输入输出完全一样
核心代码,运用递归
总结
输入输出