提交 1c440bd2 编写于 作者: L laozhang

add python 1305 and python 814

上级 38d14916
......@@ -45,6 +45,8 @@
31. [最大二叉树](./solution/tree/leetcode_654_.py)
32. [层数最深叶子节点的和](./solution/tree/leetcode_1302_.py)
33. [祖父节点值为偶数的节点和](./solution/tree/leetcode_1315_.py)
34. [两棵二叉搜索树中的所有元素](./solution/tree/leetcode_1305_.py)
35. [二叉树剪枝](./solution/tree/leetcode_814_.py)
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=utf-8
"""
1305. 两棵二叉搜索树中的所有元素
"""
from typing import List
from solution import TreeNode
class Solution:
def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:
list1 = []
list2 = []
list = []
def helper(root: TreeNode, res: List[int]):
if root:
helper(root.left, res)
res.append(root.val)
helper(root.right, res)
helper(root1, list1)
helper(root2, list2)
var1 = 0
var2 = 0
l1 = len(list1)
l2 = len(list2)
while var1 < l1 and var2 < l2:
if list1[var1] > list2[var2]:
list.append(list2[var2])
var2 += 1
else:
list.append(list1[var1])
var1 += 1
if var1 < l1:
return list + list1[var1:l1]
if var2 < l2:
return list + list2[var2:l2]
return list
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=utf-8
"""
814. 二叉树剪枝
"""
from solution import TreeNode
class Solution:
def pruneTree(self, root: TreeNode) -> TreeNode:
def helper(root: TreeNode) -> TreeNode:
if root:
helper(root.left)
helper(root.right)
if root:
if root.left and root.left.val == 0 and not root.left.left and not root.left.right:
root.left = None
if root.right and root.right.val == 0 and not root.right.right and not root.right.left:
root.right = None
helper(root)
return root
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册