From d7be863e454112fb4ec8a66da04b9b880a019b68 Mon Sep 17 00:00:00 2001 From: YDZ Date: Thu, 8 Aug 2019 21:48:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20572?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../572. Subtree of Another Tree.go | 22 +++++++ .../572. Subtree of Another Tree_test.go | 61 +++++++++++++++++++ .../0572. Subtree of Another Tree/README.md | 53 ++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 Algorithms/0572. Subtree of Another Tree/572. Subtree of Another Tree.go create mode 100644 Algorithms/0572. Subtree of Another Tree/572. Subtree of Another Tree_test.go create mode 100755 Algorithms/0572. Subtree of Another Tree/README.md diff --git a/Algorithms/0572. Subtree of Another Tree/572. Subtree of Another Tree.go b/Algorithms/0572. Subtree of Another Tree/572. Subtree of Another Tree.go new file mode 100644 index 00000000..de74977b --- /dev/null +++ b/Algorithms/0572. Subtree of Another Tree/572. Subtree of Another Tree.go @@ -0,0 +1,22 @@ +package leetcode + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func isSubtree(s *TreeNode, t *TreeNode) bool { + if isSameTree(s, t) { + return true + } + if s == nil { + return false + } + if isSubtree(s.Left, t) || isSubtree(s.Right, t) { + return true + } + return false +} diff --git a/Algorithms/0572. Subtree of Another Tree/572. Subtree of Another Tree_test.go b/Algorithms/0572. Subtree of Another Tree/572. Subtree of Another Tree_test.go new file mode 100644 index 00000000..b6530370 --- /dev/null +++ b/Algorithms/0572. Subtree of Another Tree/572. Subtree of Another Tree_test.go @@ -0,0 +1,61 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question572 struct { + para572 + ans572 +} + +// para 是参数 +// one 代表第一个参数 +type para572 struct { + s []int + t []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans572 struct { + one bool +} + +func Test_Problem572(t *testing.T) { + + qs := []question572{ + + question572{ + para572{[]int{}, []int{}}, + ans572{false}, + }, + + question572{ + para572{[]int{3, 4, 5, 1, 2}, []int{4, 1, 2}}, + ans572{true}, + }, + + question572{ + para572{[]int{1, 1}, []int{1}}, + ans572{true}, + }, + + question572{ + para572{[]int{1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, 2}, []int{1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, 2}}, + ans572{true}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 572------------------------\n") + + for _, q := range qs { + _, p := q.ans572, q.para572 + fmt.Printf("【input】:%v ", p) + roots := Ints2TreeNode(p.s) + roott := Ints2TreeNode(p.t) + fmt.Printf("【output】:%v \n", isSubtree(roots, roott)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0572. Subtree of Another Tree/README.md b/Algorithms/0572. Subtree of Another Tree/README.md new file mode 100755 index 00000000..479869a3 --- /dev/null +++ b/Algorithms/0572. Subtree of Another Tree/README.md @@ -0,0 +1,53 @@ +# [572. Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/) + + +## 题目: + +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 2 + +Given tree t: + + 4 + / \ + 1 2 + +Return **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 + / + 0 + +Given tree t: + + 4 + / \ + 1 2 + +Return **false**. + + +## 题目大意 + +给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。 + + +## 解题思路 + + +- 给出 2 棵树 `s` 和 `t`,要求判断 `t` 是否是 `s` 的子树🌲。 +- 这一题比较简单,针对 3 种情况依次递归判断,第一种情况 `s` 和 `t` 是完全一样的两棵树,第二种情况 `t` 是 `s` 左子树中的子树,第三种情况 `t` 是 `s` 右子树中的子树。第一种情况判断两棵数是否完全一致是第 100 题的原题。 +