返回介绍

lcof / 面试题26. 树的子结构 / README

发布于 2024-06-17 01:04:42 字数 7189 浏览 0 评论 0 收藏 0

面试题 26. 树的子结构

题目描述

输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)

B是A的子结构, 即 A中有出现和B相同的结构和节点值。

例如:
给定的树 A:

     3
    / \
   4   5
  / \
 1   2

给定的树 B:

   4 
  /
 1

返回 true,因为 B 与 A 的一个子树拥有相同的结构和节点值。

示例 1:

输入:A = [1,2,3], B = [3,1]
输出:false

示例 2:

输入:A = [3,4,5,1,2], B = [4,1]
输出:true

限制:

0 <= 节点个数 <= 10000

解法

方法一

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


class Solution:
  def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
    def dfs(A, B):
      if B is None:
        return True
      if A is None or A.val != B.val:
        return False
      return dfs(A.left, B.left) and dfs(A.right, B.right)

    if A is None or B is None:
      return False
    if dfs(A, B):
      return True
    return self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode(int x) { val = x; }
 * }
 */
class Solution {
  public boolean isSubStructure(TreeNode A, TreeNode B) {
    if (A == null || B == null) {
      return false;
    }
    return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
  }

  private boolean dfs(TreeNode A, TreeNode B) {
    if (B == null) {
      return true;
    }
    if (A == null || A.val != B.val) {
      return false;
    }
    return dfs(A.left, B.left) && dfs(A.right, B.right);
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
  bool isSubStructure(TreeNode* A, TreeNode* B) {
    if (!A || !B) return 0;
    return dfs(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B);
  }

  bool dfs(TreeNode* A, TreeNode* B) {
    if (!B) return 1;
    if (!A || A->val != B->val) return 0;
    return dfs(A->left, B->left) && dfs(A->right, B->right);
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func isSubStructure(A *TreeNode, B *TreeNode) bool {
  var dfs func(A, B *TreeNode) bool
  dfs = func(A, B *TreeNode) bool {
    if B == nil {
      return true
    }
    if A == nil || A.Val != B.Val {
      return false
    }
    return dfs(A.Left, B.Left) && dfs(A.Right, B.Right)
  }
  if A == nil || B == nil {
    return false
  }
  return dfs(A, B) || isSubStructure(A.Left, B) || isSubStructure(A.Right, B)
}
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *   val: number
 *   left: TreeNode | null
 *   right: TreeNode | null
 *   constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 *   }
 * }
 */

function isSubStructure(A: TreeNode | null, B: TreeNode | null): boolean {
  if (!A || !B) {
    return false;
  }
  const dfs = (A: TreeNode | null, B: TreeNode | null): boolean => {
    if (!B) {
      return true;
    }
    if (!A || A.val !== B.val) {
      return false;
    }
    return dfs(A.left, B.left) && dfs(A.right, B.right);
  };
  return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
}
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//   TreeNode {
//     val,
//     left: None,
//     right: None
//   }
//   }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
  pub fn is_sub_structure(
    a: Option<Rc<RefCell<TreeNode>>>,
    b: Option<Rc<RefCell<TreeNode>>>
  ) -> bool {
    Self::is_sub_structure_help(&a, &b)
  }

  fn is_sub_structure_help(
    a: &Option<Rc<RefCell<TreeNode>>>,
    b: &Option<Rc<RefCell<TreeNode>>>
  ) -> bool {
    if a.is_none() || b.is_none() {
      return false;
    }

    Self::dfs(a, b) ||
      Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().left, b) ||
      Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().right, b)
  }

  fn dfs(a: &Option<Rc<RefCell<TreeNode>>>, b: &Option<Rc<RefCell<TreeNode>>>) -> bool {
    if b.is_none() {
      return true;
    }
    if a.is_none() {
      return false;
    }
    let a = a.as_ref().unwrap().borrow();
    let b = b.as_ref().unwrap().borrow();
    a.val == b.val && Self::dfs(&a.left, &b.left) && Self::dfs(&a.right, &b.right)
  }
}
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *   this.val = val;
 *   this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} A
 * @param {TreeNode} B
 * @return {boolean}
 */
var isSubStructure = function (A, B) {
  if (!A || !B) {
    return false;
  }
  const dfs = (A, B) => {
    if (!B) {
      return true;
    }
    if (!A || A.val !== B.val) {
      return false;
    }
    return dfs(A.left, B.left) && dfs(A.right, B.right);
  };
  return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
};
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   public int val;
 *   public TreeNode left;
 *   public TreeNode right;
 *   public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
  public bool IsSubStructure(TreeNode A, TreeNode B) {
    if (A == null || B == null) {
      return false;
    }
    return dfs(A, B) || IsSubStructure(A.left, B) || IsSubStructure(A.right, B);
  }

  public bool dfs(TreeNode A, TreeNode B) {
    if (B == null) {
      return true;
    }
    if (A == null || A.val != B.val) {
      return false;
    }
    return dfs(A.left, B.left) && dfs(A.right, B.right);
  }
}

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

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

发布评论

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