【树-中等】面试题 04.10. 检查子树

该博客讨论了如何在大规模二叉树中判断一棵树是否为另一棵树的子树。提供了两种Python实现方法,一种使用深度优先搜索(DFS)并利用哈希计数,另一种直接递归比较节点。示例展示了不同情况下的正确输出。

题目
检查子树。你有两棵非常大的二叉树:T1,有几万个节点;T2,有几万个节点。设计一个算法,判断 T2 是否为 T1 的子树。

如果 T1 有这么一个节点 n,其子树与 T2 一模一样,则 T2 为 T1 的子树,也就是说,从节点 n 处把树砍断,得到的树与 T2 完全相同。

注意:此题相对书上原题略有改动。

示例1:

输入:t1 = [1, 2, 3], t2 = [2]
输出:true

示例2:

输入:t1 = [1, null, 2, 4], t2 = [3, 2]
输出:false

提示:

树的节点数目范围为[0, 20000]。

【代码】
【方法1】
在这里插入图片描述

class Solution:
    def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
        count = collections.Counter()
        def dfs(root,flag=1):
            if not root:
                return "#"
            serial = "{},{},{}".format(root.val, dfs(root.left), dfs(root.right))
            count[serial]+=1
            return serial
        dfs(t1)
        rs=dfs(t2,0)
        return count[rs]==2

【方法2:递归】
在这里插入图片描述

class Solution:
    def check(self,t1,t2):
        if not t1 and not t2:
            return True
        if not t1 or not t2:
            return False
        return t1.val==t2.val and self.check(t1.left,t2.left) and self.check(t1.right,t2.right)
    def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
        if not t1:
            return False
        return (self.check(t1,t2) if t1.val==t2.val else False) or self.checkSubTree(t1.left,t2) or self.checkSubTree(t1.right,t2)
ubuntu@raspberrypi:~$ bash ./PX4-Autopilot/Tools/setup/ubuntu.sh --fix-missing [ubuntu.sh] Starting... [ubuntu.sh] arch: aarch64 [ubuntu.sh] Ubuntu 20.04 [ubuntu.sh] Installing PX4 general dependencies Hit:1 http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal InRelease Hit:2 http://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu focal InRelease Hit:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal InRelease Hit:4 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal-updates InRelease Hit:5 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal-backports InRelease Hit:6 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal-security InRelease Reading package lists... Reading package lists... Building dependency tree... Reading state information... ccache is already the newest version (3.7.7-1). file is already the newest version (1:5.38-4). g++ is already the newest version (4:9.3.0-1ubuntu2). gcc is already the newest version (4:9.3.0-1ubuntu2). make is already the newest version (4.2.1-1.2). python3 is already the newest version (3.8.2-0ubuntu2). python3-dev is already the newest version (3.8.2-0ubuntu2). zip is already the newest version (3.0-11build1). astyle is already the newest version (3.1-2build1). cppcheck is already the newest version (1.90-4build1). lcov is already the newest version (1.14-2). ninja-build is already the newest version (1.10.0-1build1). shellcheck is already the newest version (0.7.0-2build2). build-essential is already the newest version (12.8ubuntu1.1). cmake is already the newest version (3.16.3-1ubuntu1.20.04.1). gdb is already the newest version (9.2-0ubuntu1~20.04.2). git is already the newest version (1:2.25.1-1ubuntu3.14). git set to manually installed. libssl-dev is already the newest version (1.1.1f-1ubuntu2.24). libxml2-dev is already the newest version (2.9.10+dfsg-5ubuntu0.20.04.10). libxml2-utils is already the newest version (2.9.10+dfsg-5ubuntu0.20.04.10). python3-setuptools is already the newest version (45.2.0-1ubuntu0.3). rsync is already the newest version (3.1.3-8ubuntu0.9). rsync set to manually installed. unzip is already the newest version (6.0-25ubuntu1.2). python3-pip is already the newest version (20.0.2-5ubuntu1.11). python3-wheel is already the newest version (0.34.2-1ubuntu0.1). 0 upgraded, 0 newly installed, 0 to remove and 163 not upgraded. [ubuntu.sh] Installing PX4 Python3 dependencies Collecting argcomplete (from -r /home/ubuntu/PX4-Autopilot/Tools/setup/requirements.txt (line 1)) Downloading argcomplete-3.6.2-py3-none-any.whl.metadata (16 kB) Collecting cerberus (from -r /home/ubuntu/PX4-Autopilot/Tools/setup/requirements.txt (line 2)) Downloading Cerberus-1.3.7-py3-none-any.whl.metadata (5.5 kB) Collecting coverage (from -r /home/ubuntu/PX4-Autopilot/Tools/setup/requirements.txt (line 3)) Downloading coverage-7.6.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.metadata (8.3 kB) Requirement already satisfied: empy<4,>=3.3 in /usr/lib/python3/dist-packages (from -r /home/ubuntu/PX4-Autopilot/Tools/setup/requirements.txt (line 4)) (3.3.2) Requirement already satisfied: future in /usr/lib/python3/dist-packages (from -r /home/ubuntu/PX4-Autopilot/Tools/setup/requirements.txt (line 5)) (0.18.2) Requirement already satisfied: jinja2>=2.8 in /usr/lib/python3/dist-packages (from -r /home/ubuntu/PX4-Autopilot/Tools/setup/requirements.txt (line 6)) (2.10.1) Requirement already satisfied: jsonschema in /usr/lib/python3/dist-packages (from -r /home/ubuntu/PX4-Autopilot/Tools/setup/requirements.txt (line 7)) (3.2.0) Collecting kconfiglib (from -r /home/ubuntu/PX4-Autopilot/Tools/setup/requirements.txt (line 8)) Downloading kconfiglib-14.1.0-py2.py3-none-any.whl.metadata (39 kB) Collecting lxml (from -r /home/ubuntu/PX4-Autopilot/Tools/setup/requirements.txt (line 9)) Downloading lxml-6.0.0.tar.gz (4.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.1/4.1 MB 4.0 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [3 lines of output] Building lxml version 6.0.0. Building without Cython. Error: Please make sure the libxml2 and libxslt development packages are installed. [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.
08-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值