给出两个二叉树,请写出一个判断两个二叉树是否相等的函数。 判断两个二叉树相等的条件是:两个二叉树的结构相同,并且相同的节点上具有相同的值。 数据范围:树上的节点数满足 , 树上节点的值满足 进阶: 空间复杂度 , 时间复杂度
示例1
输入
{1},{1}
输出
true
示例2
输入
{1,2},{1,#,2}
输出
false
加载中...
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ public class Solution { /** * * @param p TreeNode类 * @param q TreeNode类 * @return bool布尔型 */ public boolean isSameTree (TreeNode p, TreeNode q) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param p TreeNode类 * @param q TreeNode类 * @return bool布尔型 */ bool isSameTree(TreeNode* p, TreeNode* q) { // write code here } };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param p TreeNode类 # @param q TreeNode类 # @return bool布尔型 # class Solution: def isSameTree(self , p , q ): # write code here
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param p TreeNode类 * @param q TreeNode类 * @return bool布尔型 */ function isSameTree( p , q ) { // write code here } module.exports = { isSameTree : isSameTree };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param p TreeNode类 # @param q TreeNode类 # @return bool布尔型 # class Solution: def isSameTree(self , p , q ): # write code here
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * * @param p TreeNode类 * @param q TreeNode类 * @return bool布尔型 */ func isSameTree( p *TreeNode , q *TreeNode ) bool { // write code here }
{1},{1}
true
{1,2},{1,#,2}
false