原题:
Given two binary trees, write a function to check if they are equal or not.
=>给定两个二叉树,写一个函数去检查他们是否相同
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
=> 两个二叉树相同,当且仅当他们的结构相同,且节点的值也相同。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
// Note: The Solution object is instantiated only once and is reused by each test case.
}
};
晓东分析:
其实两个二叉树是否相同的判断,看起来还是蛮简单的,若用递归实现的话,就是要左,右节点作为根节点的二叉树是都是相同的,且val也要是相同的即可。
<