返回介绍

Subtree

发布于 2025-02-22 13:01:29 字数 2294 浏览 0 评论 0 收藏 0

Source

You have two every large binary trees: T1,
with millions of nodes, and T2, with hundreds of nodes.
Create an algorithm to decide if T2 is a subtree of T1.

Example
T2 is a subtree of T1 in the following case:
     1        3
    / \        /
T1 = 2   3    T2 =  4
    /
     4
T2 isn't a subtree of T1 in the following case:
     1         3
    / \         \
T1 = 2   3     T2 =  4
    /
     4
Note
A tree T2 is a subtree of T1 if there exists a node n in T1 such that
the subtree of n is identical to T2.
That is, if you cut off the tree at node n,
the two trees would be identical.

题解

判断 T2 是否是 T1 的子树,首先应该在 T1 中找到 T2 的根节点,找到根节点后两棵子树必须完全相同。所以整个思路分为两步走:找根节点,判断两棵树是否全等。咋看起来极其简单,但实际实现中还是比较精妙的,尤其是递归的先后顺序及条件与条件或的处理。

Java

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *   public int val;
 *   public TreeNode left, right;
 *   public TreeNode(int val) {
 *     this.val = val;
 *     this.left = this.right = null;
 *   }
 * }
 */
public class Solution {
  /**
   * @param T1, T2: The roots of binary tree.
   * @return: True if T2 is a subtree of T1, or false.
   */
  public boolean isSubtree(TreeNode T1, TreeNode T2) {
    if (T2 == null) return true;
    if (T1 == null) return false;
    return identical(T1, T2) || isSubtree(T1.left, T2) || isSubtree(T1.right, T2);
  }

  private boolean identical(TreeNode T1, TreeNode T2) {
    if (T1 == null && T2 == null) return true;
    if (T1 == null || T2 == null) return false;
    if (T1.val != T2.val) return false;
    return identical(T1.left, T2.left) && identical(T1.right, T2.right);
  }
}

源码分析

这道题的异常处理相对 trick 一点,需要理解 null 对子树的含义。另外需要先调用 identical 再递归调用 isSubtree 判断左右子树的情况。方法 identical 中调用 .val 前需要判断是否为 null, 而后递归调用判断左右子树是否 identical。

复杂度分析

identical 的调用,时间复杂度近似 O(n)O(n)O(n), 查根节点的时间复杂度随机,平均为 O(m)O(m)O(m), 故总的时间复杂度可近似为 O(mn)O(mn)O(mn).

Reference

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文