题目链接:https://oj.leetcode.com/problems/same-tree/
题目:
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.
解题思路:比较简单的二叉树,判断两棵树是否相等。算法上面没什么太大的说的,就是递归算法。
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if (p == NULL && q == NULL)
return true;
else if(p == NULL || q == NULL)
return false;
bool valSignal = (p->val == q->val) ? true : false;
bool leftSignal = isSameTree(p->left,q->left);
bool rightSignal = isSameTree(p->right,q->right);
return (valSignal&leftSignal&rightSignal);
}
};
转载请注明作者:vanish_dust