diff --git a/Algorithms/1123. Lowest Common Ancestor of Deepest Leaves/1123. Lowest Common Ancestor of Deepest Leaves.go b/Algorithms/1123. Lowest Common Ancestor of Deepest Leaves/1123. Lowest Common Ancestor of Deepest Leaves.go new file mode 100644 index 00000000..4459fcc2 --- /dev/null +++ b/Algorithms/1123. Lowest Common Ancestor of Deepest Leaves/1123. Lowest Common Ancestor of Deepest Leaves.go @@ -0,0 +1,31 @@ +package leetcode + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func lcaDeepestLeaves(root *TreeNode) *TreeNode { + if root == nil { + return nil + } + lca, maxLevel := &TreeNode{}, 0 + lcaDeepestLeavesDFS(&lca, &maxLevel, 0, root) + return lca +} + +func lcaDeepestLeavesDFS(lca **TreeNode, maxLevel *int, depth int, root *TreeNode) int { + *maxLevel = max(*maxLevel, depth) + if root == nil { + return depth + } + depthLeft := lcaDeepestLeavesDFS(lca, maxLevel, depth+1, root.Left) + depthRight := lcaDeepestLeavesDFS(lca, maxLevel, depth+1, root.Right) + if depthLeft == *maxLevel && depthRight == *maxLevel { + *lca = root + } + return max(depthLeft, depthRight) +} diff --git a/Algorithms/1123. Lowest Common Ancestor of Deepest Leaves/1123. Lowest Common Ancestor of Deepest Leaves_test.go b/Algorithms/1123. Lowest Common Ancestor of Deepest Leaves/1123. Lowest Common Ancestor of Deepest Leaves_test.go new file mode 100644 index 00000000..b219663d --- /dev/null +++ b/Algorithms/1123. Lowest Common Ancestor of Deepest Leaves/1123. Lowest Common Ancestor of Deepest Leaves_test.go @@ -0,0 +1,69 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question1123 struct { + para1123 + ans1123 +} + +// para 是参数 +// one 代表第一个参数 +type para1123 struct { + one []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans1123 struct { + one []int +} + +func Test_Problem1123(t *testing.T) { + + qs := []question1123{ + + question1123{ + para1123{[]int{}}, + ans1123{[]int{}}, + }, + + question1123{ + para1123{[]int{1}}, + ans1123{[]int{1}}, + }, + + question1123{ + para1123{[]int{1, 2, 3, 4}}, + ans1123{[]int{4}}, + }, + + question1123{ + para1123{[]int{1, 2, 3}}, + ans1123{[]int{1, 2, 3}}, + }, + + question1123{ + para1123{[]int{1, 2, 3, 4, 5}}, + ans1123{[]int{2, 4, 5}}, + }, + + question1123{ + para1123{[]int{1, 2, NULL, 3, 4, NULL, 6, NULL, 5}}, + ans1123{[]int{2, 3, 4, NULL, 6, NULL, 5}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 1123------------------------\n") + + for _, q := range qs { + _, p := q.ans1123, q.para1123 + fmt.Printf("【input】:%v ", p) + root := Ints2TreeNode(p.one) + fmt.Printf("【output】:%v \n", Tree2ints(lcaDeepestLeaves(root))) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/1123. Lowest Common Ancestor of Deepest Leaves/README.md b/Algorithms/1123. Lowest Common Ancestor of Deepest Leaves/README.md new file mode 100755 index 00000000..f2fd7eaa --- /dev/null +++ b/Algorithms/1123. Lowest Common Ancestor of Deepest Leaves/README.md @@ -0,0 +1,62 @@ +# [1123. Lowest Common Ancestor of Deepest Leaves](https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/) + + +## 题目: + +Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. + +Recall that: + +- The node of a binary tree is a *leaf* if and only if it has no children +- The *depth* of the root of the tree is 0, and if the depth of a node is `d`, the depth of each of its children is `d+1`. +- The *lowest common ancestor* of a set `S` of nodes is the node `A` with the largest depth such that every node in S is in the subtree with root `A`. + +**Example 1:** + + Input: root = [1,2,3] + Output: [1,2,3] + Explanation: + The deepest leaves are the nodes with values 2 and 3. + The lowest common ancestor of these leaves is the node with value 1. + The answer returned is a TreeNode object (not an array) with serialization "[1,2,3]". + +**Example 2:** + + Input: root = [1,2,3,4] + Output: [4] + +**Example 3:** + + Input: root = [1,2,3,4,5] + Output: [2,4,5] + +**Constraints:** + +- The given tree will have between 1 and 1000 nodes. +- Each node of the tree will have a distinct value between 1 and 1000. + + +## 题目大意 + + +给你一个有根节点的二叉树,找到它最深的叶节点的最近公共祖先。 + +回想一下: + +- 叶节点 是二叉树中没有子节点的节点 +- 树的根节点的 深度 为 0,如果某一节点的深度为 d,那它的子节点的深度就是 d+1 +- 如果我们假定 A 是一组节点 S 的 最近公共祖先,S 中的每个节点都在以 A 为根节点的子树中,且 A 的深度达到此条件下可能的最大值。 +  + +提示: + +- 给你的树中将有 1 到 1000 个节点。 +- 树中每个节点的值都在 1 到 1000 之间。 + + +## 解题思路 + + +- 给出一颗树,找出最深的叶子节点的最近公共祖先 LCA。 +- 这一题思路比较直接。先遍历找到最深的叶子节点,如果左右子树的最深的叶子节点深度相同,那么当前节点就是它们的最近公共祖先。如果左右子树的最深的深度不等,那么需要继续递归往下找符合题意的 LCA。如果最深的叶子节点没有兄弟,那么公共父节点就是叶子本身,否则返回它的 LCA。 +- 有几个特殊的测试用例,见测试文件。特殊的点就是最深的叶子节点没有兄弟节点的情况。