Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e261d8

Browse files
Create pseudo-palindromic-paths-in-a-binary-tree.go
1 parent 241aa35 commit 1e261d8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/
2+
3+
package pseudo_palindromic_paths_in_a_binary_tree
4+
5+
type TreeNode struct {
6+
Val int
7+
Left *TreeNode
8+
Right *TreeNode
9+
}
10+
11+
func pseudoPalindromicPaths(root *TreeNode) int {
12+
result := 0
13+
count := [10]int{}
14+
count[root.Val]++
15+
traverse(root, count, &result)
16+
return result
17+
}
18+
19+
func traverse(root *TreeNode, count [10]int, result *int) {
20+
if root.Right == nil && root.Left == nil {
21+
oddCount := 0
22+
for _, i := range count {
23+
if i%2 == 1 {
24+
oddCount++
25+
}
26+
}
27+
if oddCount < 2 {
28+
*result++
29+
}
30+
}
31+
if root.Left != nil {
32+
count[root.Left.Val]++
33+
traverse(root.Left, count, result)
34+
count[root.Left.Val]--
35+
}
36+
if root.Right != nil {
37+
count[root.Right.Val]++
38+
traverse(root.Right, count, result)
39+
count[root.Right.Val]--
40+
}
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /