leetcode专题—Subtree of Another Tree
2018-03-28 21:17
531 查看
原文:https://leetcode.com/problems/subtree-of-another-tree/description/
题目意思:判断两个二叉树种,是否存在,一个二叉树是另一二叉树的子树/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null) return false;
if (isSame(s, t)) return true;
return isSubtree(s.left, t) || isSubtree(s.right, t);
}
private boolean isSame(TreeNode s, TreeNode t) {
if (s == null && t == null) return true;
if (s == null || t == null) return false;
if (s.val != t.val) return false;
return isSame(s.left, t.left) && isSame(s.right, t.right);
}
}
题目意思:判断两个二叉树种,是否存在,一个二叉树是另一二叉树的子树/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null) return false;
if (isSame(s, t)) return true;
return isSubtree(s.left, t) || isSubtree(s.right, t);
}
private boolean isSame(TreeNode s, TreeNode t) {
if (s == null && t == null) return true;
if (s == null || t == null) return false;
if (s.val != t.val) return false;
return isSame(s.left, t.left) && isSame(s.right, t.right);
}
}
相关文章推荐
- LeetCode 572: Subtree of Another Tree
- [LeetCode] Subtree of Another Tree 另一个树的子树
- 【Leetcode-easy-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
- LeetCode Subtree of Another Tree
- LeetCode 572 Subtree of Another Tree
- LeetCode - 572 - Subtree of Another Tree
- LeetCode Subtree of Another Tree
- LeetCode: Subtree of Another Tree
- leetcode : subtree of another tree
- leetcode[Subtree of Another Tree]//待整理多种解法
- 【LeetCode】Subtree of Another Tree 解题报告
- LeetCode@Tree_572_Subtree_of_Another_Tree
- Leetcode算法学习日志-572 Subtree of Another Tree
- LeetCode.572 Subtree of Another Tree(树的序列化和子串的匹配)
- Subtree of Another Tree
- Subtree of Another Tree:判断一棵树是否是另一棵树的子树