From 02ba1d10976ea0cfb3a5e819d7682512f43df9d3 Mon Sep 17 00:00:00 2001 From: YDZ Date: Fri, 10 May 2019 19:42:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20100?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Algorithms/100. Same Tree/100. Same Tree.go | 22 ++++++ .../100. Same Tree/100. Same Tree_test.go | 71 +++++++++++++++++++ Algorithms/100. Same Tree/README.md | 51 +++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 Algorithms/100. Same Tree/100. Same Tree.go create mode 100644 Algorithms/100. Same Tree/100. Same Tree_test.go create mode 100644 Algorithms/100. Same Tree/README.md diff --git a/Algorithms/100. Same Tree/100. Same Tree.go b/Algorithms/100. Same Tree/100. Same Tree.go new file mode 100644 index 00000000..ae4056bd --- /dev/null +++ b/Algorithms/100. Same Tree/100. Same Tree.go @@ -0,0 +1,22 @@ +package leetcode + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func isSameTree(p *TreeNode, q *TreeNode) bool { + if p == nil && q == nil { + return true + } else if p != nil && q != nil { + if p.Val != q.Val { + return false + } + return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right) + } else { + return false + } +} diff --git a/Algorithms/100. Same Tree/100. Same Tree_test.go b/Algorithms/100. Same Tree/100. Same Tree_test.go new file mode 100644 index 00000000..7da83847 --- /dev/null +++ b/Algorithms/100. Same Tree/100. Same Tree_test.go @@ -0,0 +1,71 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question100 struct { + para100 + ans100 +} + +// para 是参数 +// one 代表第一个参数 +type para100 struct { + one []int + two []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans100 struct { + one bool +} + +func Test_Problem100(t *testing.T) { + + qs := []question100{ + + question100{ + para100{[]int{}, []int{}}, + ans100{true}, + }, + + question100{ + para100{[]int{}, []int{1}}, + ans100{false}, + }, + + question100{ + para100{[]int{1}, []int{1}}, + ans100{true}, + }, + + question100{ + para100{[]int{1, 2, 3}, []int{1, 2, 3}}, + ans100{true}, + }, + + question100{ + para100{[]int{1, 2}, []int{1, NULL, 2}}, + ans100{false}, + }, + + question100{ + para100{[]int{1, 2, 1}, []int{1, 1, 2}}, + ans100{false}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 100------------------------\n") + + for _, q := range qs { + _, p := q.ans100, q.para100 + fmt.Printf("【input】:%v ", p) + rootOne := Ints2TreeNode(p.one) + rootTwo := Ints2TreeNode(p.two) + fmt.Printf("【output】:%v \n", isSameTree(rootOne, rootTwo)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/100. Same Tree/README.md b/Algorithms/100. Same Tree/README.md new file mode 100644 index 00000000..77fcb793 --- /dev/null +++ b/Algorithms/100. Same Tree/README.md @@ -0,0 +1,51 @@ +# [100. Same Tree](https://leetcode.com/problems/same-tree/) + +## 题目 + + +Given two binary trees, write a function to check if they are the same or not. + +Two binary trees are considered the same if they are structurally identical and the nodes have the same value. + +Example 1: + + +```c +Input: 1 1 + / \ / \ + 2 3 2 3 + + [1,2,3], [1,2,3] + +Output: true +``` + +Example 2: + +```c +Input: 1 1 + / \ + 2 2 + + [1,2], [1,null,2] + +Output: false +``` + +Example 3: + +```c +Input: 1 1 + / \ / \ + 2 1 1 2 + + [1,2,1], [1,1,2] + +Output: false +``` + +## 题目大意 + +这一题要求判断 2 颗树是否是完全相等的。递归判断即可。 + +