951 Flip Equivalent Binary Trees

Share

Link: https://leetcode.com/problems/flip-equivalent-binary-trees/ 

Thought

If 2 nodes are equivalent, either their left and right side match, or they are flipped match. So using a recursion(why recursion? It is a Tree!), and try to match each node pairs from root1 and root2.

For each recursion level, I will first compare if current node pair have the same value. (I can return False immediately if not match). If it matches, there are 3 cases:

  1. They are identical -> recursion with (root1.left, root2.left,) and  (root1.right, root2.right) should return True
  2. They are flipped -> recursion with (root1.right, root2.left,) and  (root1.left, root2.right) should return True
  3. They are different -> Otherwise

Code

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def flipEquiv(self, root1, root2):
        """
        :type root1: TreeNode
        :type root2: TreeNode
        :rtype: bool
        """
        def compare(node1,node2):
            if node1 == None and node2 == None:
                return True
            if node1 == None or node2 == None:
                return False
            if node1.val != node2.val:
                return False
            res = compare(node1.left, node2.left) and compare(node1.right, node2.right)
            res = res or compare(node1.left, node2.right) and compare(node1.right, node2.left)
            return res
        return compare(root1, root2)

Runtime

Time: O(n)

Space: O(1)

Read more

当代码不再稀缺 - 06

Prompt 不是新的 Source Code A prompt can generate an answer. It cannot, by itself, preserve why the answer should be true. 提示词可以生成一个回答,但它自己并不能证明这个回答为什么是对的 AI coding tools 刚开始流行时,有一个说法很有吸引力: Prompt 是新的 source code。 听起来很合理。 以前我们写 Python、Java、TypeScript;以后我们写自然语言,让模型完成 implementation。既然 prompt 决定 output,那就像管理代码一样把 prompt 保存、version、review,不就行了吗?

By andy
当代码不再稀缺 - 05

当代码不再稀缺 - 05

Code review 没有过时,但我们可能 review 错了对象 AI can produce a diff. Review decides whether the team is willing to own the change. AI会产出代码,但是团队应该决定是否拥有这个改变 上一篇:AI 时代还需要 Estimate 吗? 床位系统那个 bug 暴露以后,我反复想过一个问题: 我们明明做了 code review,为什么还是没有发现这个bug? 实现并不离谱。代码在判断查询时刻是否落在预约区间内,局部逻辑说得通,边界也长得像正常的时间处理。 只是业务真正问的是“这一天是否已经被占用”,代码回答的却是“当前这一秒是否落在区间”。 我们不是没看代码。我们看了一个错误问题的正确答案。 这件事让我意识到,AI 时代关于

By andy
当代码不再稀缺 - 04

当代码不再稀缺 - 04

AI 时代还需要 Estimate 吗? Don't estimate how long it takes to generate code. Estimate what it takes to trust the change. 不要只估算生成代码需要多久,要估算团队需要付出什么,才能相信这个 change。 上一篇:别再用更多 Ticket 衡量 AI Productivity 下一篇:Code review 没有过时,但我们可能 review 错了对象 如果现在有人问 engineer:“这个 ticket 要多久?”答案可能越来越像这样: “代码今天能出来。至于什么时候敢上线,我不知道。” 这不是

By andy
当代码不再稀缺 - 03

当代码不再稀缺 - 03

别再用更多 Ticket 衡量 AI Productivity Code is cheap. Verified outcomes are not. AI 可以廉价制造代码,但不能廉价制造可信结果。 上一篇:AI 没有消灭瓶颈:Slop 正在吞掉团队的注意力 下一篇:AI 时代还需要 Estimate 吗? 有一种项目周会,特别容易让 EM 心情愉快。 这个 sprint 完成的 ticket 比以前多了,PR 数量涨了,commit 也很活跃。自从团队开始用 AI,dashboard 上的每一条线都在往右上角走。管理层一看:不错,AI productivity 已经兑现了。 然后 reviewer 默默打开

By andy