From 9ecd4e8ca46d02446179129e83e59228e8be8952 Mon Sep 17 00:00:00 2001 From: YDZ Date: Sun, 26 May 2019 10:27:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20226?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../226. Invert Binary Tree.go | 19 +++++++ .../226. Invert Binary Tree_test.go | 54 +++++++++++++++++++ Algorithms/226. Invert Binary Tree/README.md | 40 ++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 Algorithms/226. Invert Binary Tree/226. Invert Binary Tree.go create mode 100644 Algorithms/226. Invert Binary Tree/226. Invert Binary Tree_test.go create mode 100644 Algorithms/226. Invert Binary Tree/README.md diff --git a/Algorithms/226. Invert Binary Tree/226. Invert Binary Tree.go b/Algorithms/226. Invert Binary Tree/226. Invert Binary Tree.go new file mode 100644 index 00000000..e43d8b2c --- /dev/null +++ b/Algorithms/226. Invert Binary Tree/226. Invert Binary Tree.go @@ -0,0 +1,19 @@ +package leetcode + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func invertTree(root *TreeNode) *TreeNode { + if root == nil { + return nil + } + invertTree(root.Left) + invertTree(root.Right) + root.Left, root.Right = root.Right, root.Left + return root +} diff --git a/Algorithms/226. Invert Binary Tree/226. Invert Binary Tree_test.go b/Algorithms/226. Invert Binary Tree/226. Invert Binary Tree_test.go new file mode 100644 index 00000000..266c5f35 --- /dev/null +++ b/Algorithms/226. Invert Binary Tree/226. Invert Binary Tree_test.go @@ -0,0 +1,54 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question226 struct { + para226 + ans226 +} + +// para 是参数 +// one 代表第一个参数 +type para226 struct { + one []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans226 struct { + one []int +} + +func Test_Problem226(t *testing.T) { + + qs := []question226{ + + question226{ + para226{[]int{}}, + ans226{[]int{}}, + }, + + question226{ + para226{[]int{1}}, + ans226{[]int{1}}, + }, + + question226{ + para226{[]int{4, 2, 7, 1, 3, 6, 9}}, + ans226{[]int{4, 7, 2, 9, 6, 3, 1}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 226------------------------\n") + + for _, q := range qs { + _, p := q.ans226, q.para226 + fmt.Printf("【input】:%v ", p) + root := Ints2TreeNode(p.one) + fmt.Printf("【output】:%v \n", Tree2Preorder(invertTree(root))) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/226. Invert Binary Tree/README.md b/Algorithms/226. Invert Binary Tree/README.md new file mode 100644 index 00000000..6825cdcf --- /dev/null +++ b/Algorithms/226. Invert Binary Tree/README.md @@ -0,0 +1,40 @@ +# [226. Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) + +## 题目 + +Invert a binary tree. + +Example: + +Input: + +```c + 4 + / \ + 2 7 + / \ / \ +1 3 6 9 +``` + +Output: + +```c + 4 + / \ + 7 2 + / \ / \ +9 6 3 1 +``` + +Trivia: + +This problem was inspired by this original tweet by Max Howell: + +>Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off. + + +## 题目大意 + +"经典"的反转二叉树的问题。还是用递归来解决,先递归调用反转根节点的左孩子,然后递归调用反转根节点的右孩子,然后左右交换根节点的左孩子和右孩子。 + +