From 513dcf98a92e42b982b01a38da43c6aac59dfbc7 Mon Sep 17 00:00:00 2001 From: YDZ Date: Sat, 4 May 2019 06:37:35 +0400 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20104?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../104. Maximum Depth of Binary Tree.go | 16 ++++++ .../104. Maximum Depth of Binary Tree_test.go | 49 +++++++++++++++++++ .../README.md | 30 ++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 Algorithms/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.go create mode 100644 Algorithms/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree_test.go create mode 100644 Algorithms/104. Maximum Depth of Binary Tree/README.md diff --git a/Algorithms/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.go b/Algorithms/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.go new file mode 100644 index 00000000..73ca1a6e --- /dev/null +++ b/Algorithms/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.go @@ -0,0 +1,16 @@ +package leetcode + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func maxDepth(root *TreeNode) int { + if root == nil { + return 0 + } + return max(maxDepth(root.Left), maxDepth(root.Right)) + 1 +} diff --git a/Algorithms/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree_test.go b/Algorithms/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree_test.go new file mode 100644 index 00000000..3a79f400 --- /dev/null +++ b/Algorithms/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree_test.go @@ -0,0 +1,49 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question104 struct { + para104 + ans104 +} + +// para 是参数 +// one 代表第一个参数 +type para104 struct { + one []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans104 struct { + one int +} + +func Test_Problem104(t *testing.T) { + + qs := []question104{ + + question104{ + para104{[]int{}}, + ans104{0}, + }, + + question104{ + para104{[]int{3, 9, 20, NULL, NULL, 15, 7}}, + ans104{3}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 104------------------------\n") + + for _, q := range qs { + _, p := q.ans104, q.para104 + fmt.Printf("【input】:%v ", p) + root := Ints2TreeNode(p.one) + fmt.Printf("【output】:%v \n", maxDepth(root)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/104. Maximum Depth of Binary Tree/README.md b/Algorithms/104. Maximum Depth of Binary Tree/README.md new file mode 100644 index 00000000..c7bb17e6 --- /dev/null +++ b/Algorithms/104. Maximum Depth of Binary Tree/README.md @@ -0,0 +1,30 @@ +# [104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) + +## 题目 + +Given a binary tree, find its maximum depth. + +The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. + +Note: A leaf is a node with no children. + +Example: + +Given binary tree [3,9,20,null,null,15,7], + +```c + 3 + / \ + 9 20 + / \ + 15 7 +``` + +return its depth = 3. + + +## 题目大意 + +要求输出一棵树的最大高度。这一题递归遍历就可,遍历根节点的左孩子的高度和根节点右孩子的高度,取出两者的最大值再加一即为总高度。 + +