关闭

leetcode 100 Same Tree

76人阅读 评论(0) 收藏 举报
分类:

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.

Subscribe to see which companies asked this question


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool same(TreeNode *p, TreeNode *q) {
		if(p==NULL && q==NULL) return true;
		if(p==NULL || q==NULL) return false;
		if(p->val!=q->val) return false;
		if(!same(p->left, q->left)) return false;
		return same(p->right, q->right);
	}
	bool isSameTree(TreeNode *p, TreeNode *q) {
		return same(p, q);
	}
};




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:110011次
    • 积分:4670
    • 等级:
    • 排名:第6199名
    • 原创:378篇
    • 转载:4篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论