给定两个根结点分别为 和 二叉树,请判断这两棵树是否完全相同。
数据范围:
两棵树上的节点数目都在范围 [0, 100] 内
bool isSameTree(struct TreeNode* root1, struct TreeNode* root2 ) { if(root1==NULL&&root2==NULL)return true; if(root1&&root2==NULL)return false; if(root1==NULL&&root2)return false; if(root1->val!=root2->val)return false; return isSameTree(root1->left, root2->left)&&isSameTree(root1->right, root2->right); }送给考研C
论 Eq 特型的正确用法~
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option<Box<TreeNode>>, * pub right: Option<Box<TreeNode>>, * } * * impl TreeNode { * #[inline] * fn new(val: i32) -> Self { * TreeNode { * val: val, * left: None, * right: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ pub fn isSameTree(&self, root1: Option<Box<TreeNode>>, root2: Option<Box<TreeNode>>) -> bool { // write code here root1 == root2 } }
package main //import "fmt" import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ func isSameTree( root1 *TreeNode , root2 *TreeNode ) bool { if root1==nil&&root2==nil{ return true }else if root1==nil||root2==nil||root1.Val!=root2.Val{ return false }else{ return isSameTree(root1.Left,root2.Left)&&isSameTree(root1.Right,root2.Right) } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ bool preorder(struct TreeNode* root1, struct TreeNode* root2) { //static int count = 0; if (NULL != root1 && NULL != root2) { if (root1->val != root2->val) return false; return preorder(root1->left, root2->left)&&preorder(root1->right, root2->right) ; // preorder(root1->left, root2->left); // preorder(root1->right, root2->right); } if (NULL != root1 && NULL == root2) return false; if (NULL == root1 && NULL != root2) return false; return true; } bool isSameTree(struct TreeNode* root1, struct TreeNode* root2 ) { // write code here // if(!root1 && !root2) // return true; // if(!root1 || !root2) // return false; // if(root1->val!=root2->val) // return false; // return isSameTree(root1->left,root2->left)&&isSameTree(root1->right,root2->right); return preorder(root1, root2); }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ bool isSameStucture(TreeNode* p1, TreeNode* p2) { // check root if (p1 == nullptr && p2 == nullptr) { return true; } else if (p1 == nullptr || p2 == nullptr) { return false; } // check left child if (p1 -> left == nullptr && p2 -> left == nullptr) { ; } else if (p1 -> left == nullptr || p2 -> left == nullptr) { return false; } // check right child if (p1 -> right == nullptr && p2 -> right == nullptr) { ; } else if (p1 -> right == nullptr || p2 -> right == nullptr) { return false; } return true; } bool isSameTree(TreeNode* root1, TreeNode* root2) { // write code here if (!isSameStucture(root1, root2)) { return false; } stack<TreeNode*> s1, s2; s1.push(root1); s2.push(root2); TreeNode* cur1 = nullptr, *cur2 = nullptr; while (!s1.empty() || !s2.empty()) { cur1 = s1.top(); s1.pop(); cur2 = s2.top(); s2.pop(); while (cur1 != nullptr || cur2 != nullptr) { if (!isSameStucture(cur1, cur2)) { return false; } // visit nodes if (cur1 -> val != cur2 -> val) { return false; } // push right child into stack if (cur1 -> right != nullptr) { s1.push(cur1 -> right); s2.push(cur2 -> right); } cur1 = cur1 -> left; cur2 = cur2 -> left; } } return true; } };
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ public boolean isSameTree (TreeNode root1, TreeNode root2) { // write code here Queue<TreeNode> rootQueue1 = new LinkedList<>(); Queue<TreeNode> rootQueue2 = new LinkedList<>(); rootQueue1.offer(root1); rootQueue2.offer(root2); while (!rootQueue1.isEmpty() && !rootQueue2.isEmpty()) { TreeNode poll1 = rootQueue1.poll(); TreeNode poll2 = rootQueue2.poll(); if(poll1 == null && poll2 == null) { continue; } if (poll1 == null || poll2 == null || poll1.val != poll2.val) { return false; } rootQueue1.offer(poll1.left); rootQueue1.offer(poll1.right); rootQueue2.offer(poll2.left); rootQueue2.offer(poll2.right); } return true; } }
bool isSameTree(TreeNode* root1, TreeNode* root2) { if(!root1&&!root2)return true; if(!root1||!root2)return false; if(root1->val!=root2->val)return false; return isSameTree(root1->left,root2->left)&&isSameTree(root1->right,root2->right); }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ public boolean isSameTree (TreeNode root1, TreeNode root2) { if(root1 != null && root2 == null || root1 == null && root2 != null) return false; if(root1 == null && root2 == null) return true; if(root1.val != root2.val) return false; return isSameTree(root1.left,root2.left) && isSameTree(root1.right,root2.right); } }