等价二叉树
来源:互联网 发布:数据治理整体解决方案 编辑:程序博客网 时间:2024/06/12 23:47
检查两棵二叉树是否等价。等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等。
样例
1 1 / \ / \ 2 2 and 2 2 / /4 4
就是两棵等价的二叉树。
1 1 / \ / \ 2 3 and 2 3 / \4 4
就不是等价的。
解题思路:建立一个flag,直接进行检验每个结点是否一样,若出现不一样则直接跳出。
class Solution {public: /** * @aaram a, b, the root of binary trees. * @return true if they are identical, or false. */ bool ans=1; bool isIdentical(TreeNode* a, TreeNode* b) { // Write your code here if(ans==0)return ans; if(a!=NULL&&b!=NULL){ if(a->val!=b->val){ ans=0; return 0; } else { isIdentical(a->left,b->left); isIdentical(a->right,b->right); return ans; } } if(a!=NULL&&b==NULL){ans=0;return 0;} if(b!=NULL&&a==NULL){ans=0;return 0;} return ans; }};
0 0
- LintCode_469_等价二叉树
- LintCode:等价二叉树
- lintcode ----等价二叉树
- 等价二叉树
- 等价二叉树-lintcode
- 等价二叉树,lintcode
- lintcode-->等价二叉树
- lintcode,等价二叉树
- 等价二叉树
- LintCode-等价二叉树
- 等价二叉树
- 等价二叉树
- 等价二叉树
- LIntCode 等价二叉树
- 等价二叉树
- 等价二叉树-LintCode
- 等价二叉树
- 等价二叉树
- ORAchk使用
- 学习PHP表单时的一些小问题
- 浅谈Spring与web.xml
- JDBC简介
- KMP算法
- 等价二叉树
- BZOJ 1552 [Cerc2007]robotic sort
- 配置电脑网络IP
- Java面试准备二:String、StringBuilder、StringBuffer
- 【笔试】Java中的重载与重写
- Dokcer创建私有仓库、私有仓库Web管理
- 陈独秀先生介绍
- c++ 中 毫秒级时间获取
- 4月11日,MyThread,每日20行。