<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">题目链接:</span><a target=_blank href="https://oj.leetcode.com/problems/same-tree/" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">https://oj.leetcode.com/problems/same-tree/</a>
题目:
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);
}
};
原文地址:http://blog.csdn.net/vanish_dust/article/details/43283959