LintCode 子树
来源:互联网 发布:mysql删除外键索引 编辑:程序博客网 时间:2024/05/21 21:35
容易 子树
19%
通过
有两个不同大小的二进制树: T1
有上百万的节点; T2
有好几百的节点。请设计一种算法,判定 T2
是否为 T1
的子树。
Yes
样例
下面的例子中 T2 是 T1 的子树:
1 3 / \ / T1 = 2 3 T2 = 4 / 4
下面的例子中 T2 不是 T1 的子树:
1 3 / \ \T1 = 2 3 T2 = 4 / 4
/** * 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: /** * @param T1, T2: The roots of binary tree. * @return: True if T2 is a subtree of T1, or false. */ bool isSubtree(TreeNode *T1, TreeNode *T2) { bool result = false; if (T2 == nullptr) { return true; } if (T1 == nullptr) { return false; } // write your code here if (T1->val == T2->val) { result = dp(T1,T2); } if (!result) { result = isSubtree(T1->left,T2); } if (!result) { result = isSubtree(T1->right,T2); } return result; } bool dp (TreeNode *T1, TreeNode *T2) { if (T1 != nullptr && T2!=nullptr && T1->val == T2->val) { return dp(T1->left,T2->left) && dp (T1->right,T2->right); } if (T1 == nullptr && T2 == nullptr) { return true; } return false; }};
0 0
- LintCode 子树
- lintcode-子树
- lintcode ----子树
- lintcode,子树
- LintCode 子树
- 子树-LintCode
- LintCode-子树
- Lintcode 子树
- lintcode子树
- LintCode 子树
- LintCode 子树
- 子树-LintCode
- 子树(LintCode)
- lintcode subtree 子树
- lintcode——子树
- [LintCode]245.子树
- LintCode-剑指Offer-(245)子树
- lintcode-递归-简单-245子树
- C语言关键字auto与register的深入理解
- 浅谈批处理中的%cd%与%~dp0
- js闭包的理解
- Java中基础类库使用
- Microsoft RemoteFX简介
- LintCode 子树
- 键、索引、约束及其区别
- android interpolator 插值器
- 网易(weather)天气预报接口
- Object转String的三种方式比较
- 自定义头像处理,轻巧实用,很多强大的小技巧在里面哦
- GRE阅读难句教程(一)
- iOS 画图 图片圆形处理
- Android adb shell kill PID