输入两棵二叉树A,B,判断B是不是A的子结构。(我们约定空树不是任意一个树的子结构) 假如给定A为{8,8,7,9,2,#,#,#,#,4,7},B为{8,9,2},2个树的结构如下,可以看出B是A的子结构 数据范围: 0 0
示例1
输入
{8,8,7,9,2,#,#,#,#,4,7},{8,9,2}
输出
true
示例2
输入
{1,2,3,4,5},{2,4}
输出
true
示例3
输入
{1,2,3},{3,1}
输出
false
加载中...
import java.util.*; /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public boolean HasSubtree(TreeNode root1,TreeNode root2) { } }
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) { } };
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def HasSubtree(self, pRoot1, pRoot2): # write code here
/* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } }*/ class Solution { public bool HasSubtree(TreeNode pRoot1, TreeNode pRoot2) { // write code here } }
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function HasSubtree(pRoot1, pRoot2) { // write code here } module.exports = { HasSubtree : HasSubtree };
val = $val; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pRoot1 TreeNode类 * @param pRoot2 TreeNode类 * @return bool布尔型 */ function HasSubtree( $pRoot1 , $pRoot2 ) { // write code here }
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot1 TreeNode类 # @param pRoot2 TreeNode类 # @return bool布尔型 # class Solution: def HasSubtree(self , pRoot1: TreeNode, pRoot2: TreeNode) -> bool: # write code here
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pRoot1 TreeNode类 * @param pRoot2 TreeNode类 * @return bool布尔型 */ func HasSubtree( pRoot1 *TreeNode , pRoot2 *TreeNode ) bool { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot1 TreeNode类 * @param pRoot2 TreeNode类 * @return bool布尔型 */ bool HasSubtree(struct TreeNode* pRoot1, struct TreeNode* pRoot2 ) { // write code here }
# class TreeNode # attr_accessor :val, :left, :right # def initialize(val, left = nil, right = nil) # @val, @left, @right = val, left, right # end # end # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param pRoot1 TreeNode类 # @param pRoot2 TreeNode类 # @return bool布尔型 # class Solution def HasSubtree(pRoot1, pRoot2) # write code here end end
/** * class TreeNode(var val: Int) { * var left: TreeNode = null * var right: TreeNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pRoot1 TreeNode类 * @param pRoot2 TreeNode类 * @return bool布尔型 */ def HasSubtree(pRoot1: TreeNode,pRoot2: TreeNode): Boolean = { // write code here } }
/** * class TreeNode(var `val`: Int) { * var left: TreeNode? = null * var right: TreeNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pRoot1 TreeNode类 * @param pRoot2 TreeNode类 * @return bool布尔型 */ fun HasSubtree(pRoot1: TreeNode?,pRoot2: TreeNode?): Boolean { // write code here } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pRoot1 TreeNode类 * @param pRoot2 TreeNode类 * @return bool布尔型 */ public boolean HasSubtree (TreeNode pRoot1, TreeNode pRoot2) { // write code here } }
/*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) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pRoot1 TreeNode类 * @param pRoot2 TreeNode类 * @return bool布尔型 */ export function HasSubtree(pRoot1: TreeNode, pRoot2: TreeNode): boolean { // write code here }
/** * public class TreeNode { * public var val: Int * public var left: TreeNode? * public var right: TreeNode? * public init(_ val: Int=0, _ left: TreeNode?=nil, _ right: TreeNode?=nil) { * self.val = val * self.left = left * self.right = right * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pRoot1 TreeNode类 * @param pRoot2 TreeNode类 * @return bool布尔型 */ func HasSubtree ( _ pRoot1: TreeNode?, _ pRoot2: TreeNode?) -> Bool { // write code here } }
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option
>, * pub right: Option
>, * } * * impl TreeNode { * #[inline] * fn new(val: i32) -> Self { * TreeNode { * val: val, * left: None, * right: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pRoot1 TreeNode类 * @param pRoot2 TreeNode类 * @return bool布尔型 */ pub fn HasSubtree(&self, pRoot1: Option
>, pRoot2: Option
>) -> bool { // write code here } }
{8,8,7,9,2,#,#,#,#,4,7},{8,9,2}
true
{1,2,3,4,5},{2,4}
true
{1,2,3},{3,1}
false