设为首页 加入收藏

TOP

leetcode - Same Tree
2015-07-20 17:35:58 来源: 作者: 【 】 浏览:2
Tags:leetcode 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.

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
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) {
		if(p == NULL && q == NULL) return true;
		if((p == NULL&&q != NULL)||(p != NULL&&q == NULL)||(p->val != q->val)) return false;
		return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
    }
};


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU-5045 Contest 状态压缩DP求期.. 下一篇HDU - 5045 Contest(DP+状压)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)