LeetCode Same Tree 决定是否是相同的树
来源:互联网 发布:手机购买汽车票软件 编辑:程序博客网 时间:2024/05/21 22:27
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.
检查两颗二叉树是否一样,基本上都是一个套路,这里稍微不一样的就是递归到每一个节点都需要判断是否一样。
有一个节点不一样就可以判断不是一样的二叉树了。如果递归完了还是一样,那么就是一样的两颗二叉树了。这也是为什么判断变量isSame初始化为true了。
/** * Definition for binary tree * 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) { bool isSame = true; HelperSame(p, q, isSame); return isSame; } void HelperSame(TreeNode *p, TreeNode *q, bool &isSame) { if(!p && !q) return; if (!p&&q || p&&!q) { isSame = false; return; } if (q->val != p->val) { isSame = false; return; } if (p && q) HelperSame(p->left, q->left, isSame); if (p && q) HelperSame(p->right, q->right, isSame); }};
//2014-2-15 updatebool isSameTree(TreeNode *p, TreeNode *q) {if (!p && !q) return true;if (!p && q || p && !q || p->val != q->val) return false;return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);}
- LeetCode Same Tree 决定是否是相同的树
- Same Tree 比较是否是相同的树@ LeetCode
- 【leetcode】比较两棵二叉树是否相同(Same Tree)
- Leetcode Same Tree 判断两棵二叉树是否相同
- Leetcode 100. Same Tree 验证树是否相同 解题报告
- [LeetCode]100. Same Tree--判断二叉树是否相同
- LeetCode--Same Tree(判断两个二叉树是否相同)Python
- 100. Same Tree 树是否相同
- LeetCode OJ 之 Same Tree (相同树的判断)
- LeetCode 100. Same Tree(相同的树)
- (二叉树是否相同或者堆成)LeetCode#100. Same Tree #101. Symmetric Tree
- [LeetCode]100. Same Tree(相同树)
- Same Tree-相同树
- Same Tree 判断相同的树
- Same Tree--比较两个二叉树是否相同
- Same Tree:判断两棵二叉树是否相同
- Leetcode#100. Same Tree(判断两个二叉树相同)
- Leetcode刷题记——100. Same Tree(相同的树)
- 在django xadmin中使用 Ueditor
- 在VS2010下打开VS2008项目的解决办法
- mysql中用命令行复制表结构的方法
- 问题随笔记
- 滑雪(动态规划+dfs)
- LeetCode Same Tree 决定是否是相同的树
- 2011斯坦福大学iOS应用开发教程学习笔记(第八课)viewController生命周期
- 关于spss显示“试图连接远程服务器失败”的问题以及解决方法
- ecmall商家手机短信功能插件
- Python关键字yield详解
- 图像分割
- [翻译Joel On Software]无痛功能规范 – 第三部分:不过…如何去做?/Painless Functional Specifications - Part 3: But... How?
- 反射中的Constructor和数组反射
- PHP 执行远程文件 curl