【LeetCode】572. Subtree of Another Tree
来源:互联网 发布:淘宝led散热片 编辑:程序博客网 时间:2024/06/05 01:18
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.
Example 1:
Given tree s:
3 / \ 4 5 / \ 1 2Given tree t:
4 / \ 1 2Return true, because t has the same structure and node values with a subtree of s.
Example 2:
Given tree s:
3 / \ 4 5 / \ 1 2 / 0Given tree t:
4 / \ 1 2Return false.
题目是判断二叉树t是不是二叉树s的子树。
class Solution {public: bool isSameTree(TreeNode* s, TreeNode* t){ if(!s&&!t)return true;//两个都未空,说明到叶子节点 else if(!s||!t)return false;//只有一个节点为空,不相等 else return s->val==t->val&&isSameTree(s->left,t->left)&&isSameTree(s->right,t->right); } bool isSubtree(TreeNode* s, TreeNode* t) { if(!s)return false; if(isSameTree(s,t))return true; else return isSubtree(s->left,t)||isSubtree(s->right,t); }};
阅读全文
0 0
- [LeetCode]572. Subtree of Another Tree
- [leetcode]: 572. Subtree of Another Tree
- leetcode 572. Subtree of Another Tree
- [leetcode: Python]572. Subtree of Another Tree
- [leetcode]572. Subtree of Another Tree
- LeetCode 572. Subtree of Another Tree
- [LeetCode] 572. Subtree of Another Tree
- LeetCode-572. Subtree of Another Tree (Java)
- Leetcode-572. Subtree of Another Tree
- leetcode 572. Subtree of Another Tree
- LeetCode 572. Subtree of Another Tree
- [LeetCode]572. Subtree of Another Tree
- 【LeetCode】572. Subtree of Another Tree
- leetcode 572. Subtree of Another Tree
- leetcode#572. Subtree of Another Tree
- LeetCode 572. Subtree of Another Tree
- LeetCode刷题 | 572. Subtree of Another Tree
- LeetCode: Subtree of Another Tree
- keilc 4 编译出现的几个错误解决
- QT5.9 在线程中导出EXCLE报 CoCreateInstance failure (尚未调用 CoInitialize) 解决办法
- CFGym 101002C 状压DP
- oracle内置函数
- python之Flask(一)
- 【LeetCode】572. Subtree of Another Tree
- HDU
- 【腾讯笔试】满二叉排序树中查找三个节点的最小子树的根节点
- linux命令-sed命令使用(1)
- python中mysql基本操作
- MongoDB C++开发环境搭建
- 分布式消息系统:Kafka
- 使用Maven运行Java main的3种方式
- 数据控制、定义、操作