小黑代码(小黑卡在了bug中,上午一步步探索做出,非常NB!!!)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rob(self, root: Optional[TreeNode]) -> int:
# 递归函数进行动态规划
def dfs(node):
# 节点为空
if not node:
return 0, 0, 0
# 叶子结点
if not (node.left or node.right):
return 0, 0, node.val
# 动态规划操作
first_1, second_1, node1_val = dfs(node.left)
first_2, second_2, node2_val = dfs(node.right)
third_1 = max(second_1, first_1+node1_val)
third_2 = max(second_2, first_2+node2_val)
return second_1+second_2, third_1+third_2, node.val
first_root, second_root, val = dfs(root)
return max(second_root, first_root+val)
动态规划法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rob(self, root: Optional[TreeNode]) -> int:
# 递归函数
# 返回[不选择该结点获得收益, 选择该结点获得的收益]
def dfs(node):
# 结点为空
if not node:
return [0, 0]
# 获得左右结点的数组
l = dfs(node.left)
r = dfs(node.right)
# 不选择该结点
not_select = max(l[0], l[1]) + max(r[0], r[1])
# 选择该结点
select = node.val + l[0] + r[0]
return [not_select, select]
return max(dfs(root))
数据库练习
608. 树节点
使用union
# Write your MySQL query statement below
SELECT
id, 'Root' AS 'type'
FROM
Tree
WHERE
p_id IS NULL
UNION
SELECT
id, 'Leaf' AS 'type'
FROM
Tree
WHERE
id NOT IN
(SELECT
p_id
FROM
Tree
WHERE
p_id IS NOT NULL
)
AND p_id IS NOT NULL
UNION
SELECT
id, 'Inner' AS 'type'
FROM
Tree
WHERE
id IN
(SELECT DISTINCT
p_id
FROM
Tree
WHERE
p_id IS NOT NULL
)
AND p_id IS NOT NULL
Case法(小黑第一次使用,了解了sql中的case的基本用法)
# Write your MySQL query statement below
SELECT
id,
CASE
WHEN tree.p_id IS NULL
THEN 'Root'
WHEN tree.id IN (SELECT DISTINCT p_id FROM Tree WHERE p_id IS NOT NULL) AND tree.p_id IS NOT NULL
THEN 'Inner'
WHEN tree.id NOT IN (SELECT DISTINCT p_id FROM TREE WHERE p_id IS NOT NULL)
THEN 'Leaf'
ELSE 'other'
END AS Type
FROM
Tree
IF分支法
# Write your MySQL query statement below
SELECT
id,
IF(ISNULL(Tree.p_id), 'Root', IF(Tree.id IN (SELECT DISTINCT p_id FROM Tree WHERE p_id IS NOT NULL) AND Tree.p_id IS NOT NULL, 'Inner', 'Leaf')) AS 'type'
FROM
Tree
小黑生活
边看店边学科一
看了看《强风吹拂》,来了点儿感觉
晚饭我弟弟买了肚
从我姥姥家回家
早上尝试五点起床跑个步,肚子比较难受![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a48cec0e06f99db4c938f1e09a781d37.png)
早餐混沌
早上同事给了我一块儿糖
买杯咖啡
早上忘记刮胡子了,外卖叫了个刮胡刀
午餐干饭
中午一起练歌,第一次进入部室馆
填写了干部履历表
学习深度强化学习的策略迭代部分
下班阳光照进了办公室
练完了琴来份烤冷面
我的快递到了,能量胶和正山小种茶
还是查不到社保公积金数据
早上继续五点起来跑步,天上下雨了,终于意识到以后不空腹吃能量胶了,感觉对胃的刺激还是比较大的
早上把茶带到了单位,正山小种
继续学习强化学习
午餐干饭,今天又打多了,剩了一点儿,明天一定少打点儿好好养养胃
练完歌在咖啡厅里买杯冰镇柚子茶聊聊天