提交 9ea276c8 编写于 作者: L laozhang

add python 513 and python 222

上级 28a8aad6
......@@ -53,6 +53,8 @@
39. [先序遍历构造二叉树](./solution/tree/leetcode_1008_.py)
40. [二叉搜索树中第K小的元素](./solution/tree/leetcode_230_.py)
41. [删除给定值的叶子节点](./solution/tree/leetcode_1325_.py)
42. [找树左下角的值](./solution/tree/leetcode_513_.py)
43. [完全二叉树的节点个数](./solution/tree/leetcode_222_.py)
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=utf-8
"""
222. 完全二叉树的节点个数
"""
from solution import TreeNode
class Solution:
def countNodes(self, root: TreeNode) -> int:
if not root:
return 0
a = self.countNodes(root.left)
b = self.countNodes(root.right)
return a+b+1
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=utf-8
"""
513. 找树左下角的值
"""
from solution import TreeNode
class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
list=[]
def helper(root:TreeNode,k:int):
nonlocal list
if root:
if k>len(list):
temp=[]
temp.append(root.val)
list.append(temp)
else:
list[k-1].append(root.val)
helper(root.left,k+1)
helper(root.right,k+1)
helper(root,1)
return list[-1][0]
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册