Lintcode 等价二叉树
来源:互联网 发布:淘宝上的祛痘产品 编辑:程序博客网 时间:2024/05/17 02:29
1:问题描述
检查两棵二叉树是否等价。等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等。
样例
1 1 / \ / \ 2 2 and 2 2 / /4 4
就是两棵等价的二叉树。
1 1 / \ / \ 2 3 and 2 3 / \4 4
2:解题思路
从根节点开始判断两颗树是否相等,先判断结构是否相等,在判断左右子树上的值是否相等,在用递归的思想判断左右子树。
3:解题代码
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @aaram a, b, the root of binary trees.
* @return true if they are identical, or false.
*/
bool isIdentical(TreeNode* a, TreeNode* b) {
if(a==NULL&&b==NULL)return 1;
if((a!=NULL&&b==NULL)||(a==NULL&&b!=NULL))return 0;
if(a->val==b->val)
{
return isIdentical(a->left, b->left) && isIdentical(a->right, b->right);
}
return 0;
}
};
4:感想
这道题还是比较简单,题目介绍的也很清楚,思路容易想到,就是判断树的结构还有对应结构的值是否相同就可以。
- LintCode:等价二叉树
- lintcode ----等价二叉树
- 等价二叉树-lintcode
- 等价二叉树,lintcode
- lintcode-->等价二叉树
- lintcode,等价二叉树
- LintCode-等价二叉树
- LIntCode 等价二叉树
- 等价二叉树-LintCode
- Lintcode 等价二叉树
- LintCode 等价二叉树
- 等价二叉树-LintCode
- LintCode - 469.等价二叉树
- lintcode 469 等价二叉树
- LintCode 469-等价二叉树
- [LintCode]469.等价二叉树
- [Lintcode] #469 等价二叉树
- lintCode(469)——等价二叉树
- 堆排序解剖
- ALL in ALL
- 使用Java实现面向对象编程-->抽象和封装
- Mysql utf8 格式 升级为 utf8mb4
- LeetCode 234.Palindrome Linked List
- Lintcode 等价二叉树
- 莫队算法
- 静态库基础
- mysql数据库优化
- 图论-双连通分量
- 拍照扫描银行银行卡识别软件SDK
- HDOJ 1102 Constructing Roads(并查集入门)
- 动态库基础
- POJ-1182 食物链