Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 084e3a9

Browse files
author
侯利朋
committed
ok
1 parent f67efa0 commit 084e3a9

5 files changed

+185
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode id=1123 lang=golang
3+
*
4+
* [1123] Lowest Common Ancestor of Deepest Leaves
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
// (8 ms) √ Your runtime beats 90.91 %
15+
// 返回子树深度相同的节点
16+
func lcaDeepestLeaves(root *TreeNode) *TreeNode {
17+
if root == nil {
18+
return root
19+
}
20+
ldep := depth(root.Left)
21+
rdep := depth(root.Right)
22+
if ldep == rdep {
23+
return root
24+
} else if ldep > rdep {
25+
return lcaDeepestLeaves(root.Left)
26+
}
27+
return lcaDeepestLeaves(root.Right)
28+
}
29+
30+
func depth(root *TreeNode) int {
31+
if root == nil {
32+
return 0
33+
}
34+
l := depth(root.Left)
35+
r := depth(root.Right)
36+
if l > r {
37+
return l + 1
38+
}
39+
return r + 1
40+
}

508.most-frequent-subtree-sum.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode id=508 lang=golang
3+
*
4+
* [508] Most Frequent Subtree Sum
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
// 12 ms, faster than 68.85%
15+
// map缓存所有结果
16+
func findFrequentTreeSum(root *TreeNode) []int {
17+
tmp := make(map[int]int)
18+
sum(root, tmp)
19+
maxTime := -1
20+
rtn := []int{}
21+
for k, v := range tmp {
22+
if v > maxTime {
23+
maxTime = v
24+
rtn = append(rtn, k)
25+
rtn = rtn[len(rtn)-1:]
26+
} else if v == maxTime {
27+
rtn = append(rtn, k)
28+
}
29+
}
30+
return rtn
31+
}
32+
33+
func sum(root *TreeNode, tmp map[int]int) int {
34+
if root == nil {
35+
return 0
36+
}
37+
l, r := 0, 0
38+
if root.Left != nil {
39+
l = sum(root.Left, tmp)
40+
}
41+
if root.Right != nil {
42+
r = sum(root.Right, tmp)
43+
}
44+
45+
tmp[l+r+root.Val]++ // 只在此处统计一次结果,在上面的left或right判断中不记录,否则会导致多计算一次
46+
return l + r + root.Val
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode id=865 lang=golang
3+
*
4+
* [865] Smallest Subtree with all the Deepest Nodes
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
// (0 ms)
15+
func subtreeWithAllDeepest(root *TreeNode) *TreeNode {
16+
if root == nil {
17+
return root
18+
}
19+
ldep := depth(root.Left)
20+
rdep := depth(root.Right)
21+
if ldep == rdep {
22+
return root
23+
} else if ldep > rdep {
24+
return subtreeWithAllDeepest(root.Left)
25+
}
26+
return subtreeWithAllDeepest(root.Right)
27+
}
28+
29+
func depth(root *TreeNode) int {
30+
if root == nil {
31+
return 0
32+
}
33+
l := depth(root.Left)
34+
r := depth(root.Right)
35+
return max(l, r) + 1
36+
}
37+
38+
func max(a, b int) int {
39+
if a > b {
40+
return a
41+
}
42+
return b
43+
}

951.flip-equivalent-binary-trees.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @lc app=leetcode id=951 lang=golang
3+
*
4+
* [951] Flip Equivalent Binary Trees
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
// (0 ms)
15+
func flipEquiv(root1 *TreeNode, root2 *TreeNode) bool {
16+
if root1 == nil && root2 == nil {
17+
return true
18+
}
19+
if root1 == nil || root2 == nil {
20+
return false
21+
}
22+
if root1.Val != root2.Val {
23+
return false
24+
}
25+
return (flipEquiv(root1.Left, root2.Left) && flipEquiv(root1.Right, root2.Right)) ||
26+
(flipEquiv(root1.Left, root2.Right) && flipEquiv(root1.Right, root2.Left))
27+
}

998.maximum-binary-tree-ii.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode id=998 lang=golang
3+
*
4+
* [998] Maximum Binary Tree II
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
// 0 ms
15+
func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
16+
if root == nil {
17+
return &TreeNode{val, nil, nil}
18+
}
19+
if val > root.Val {
20+
rtn := &TreeNode{}
21+
rtn.Val = val
22+
rtn.Left = root
23+
return rtn
24+
}
25+
// 新的节点插入右子树,同时注意树的连接,不能断开
26+
root.Right = insertIntoMaxTree(root.Right, val)
27+
return root
28+
}

0 commit comments

Comments
 (0)