提交 81ad6513 编写于 作者: L laozhang

add python 1325 and python 230

上级 db53f164
......@@ -51,6 +51,8 @@
37. [二叉搜索树迭代器](./solution/tree/leetcode_173_.py)
38. [二叉树的中序遍历](./solution/tree/leetcode_94_.py)
39. [先序遍历构造二叉树](./solution/tree/leetcode_1008_.py)
40. [二叉搜索树中第K小的元素](./solution/tree/leetcode_230_.py)
41. [删除给定值的叶子节点](./solution/tree/leetcode_1325_.py)
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=utf-8
"""
1325. 删除给定值的叶子节点
"""
from solution import TreeNode
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def removeLeafNodes(self, root: TreeNode, target: int) -> TreeNode:
if not root:
return None
root.left = self.removeLeafNodes(root.left, target)
root.right = self.removeLeafNodes(root.right, target)
if not root.left and not root.right and root.val == target:
return None
else:
return root
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=utf-8
"""
230. 二叉搜索树中第K小的元素
"""
from solution import TreeNode
class Solution:
def kthSmallest(self, root: TreeNode, k: int) -> int:
count = 0
min = 0
def helper(root: TreeNode, k: int):
nonlocal count, min
if root:
helper(root.left, k)
count += 1
if count == k:
min = root.val
helper(root.right, k)
helper(root, k)
return min
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册