Skip to content

Commit 28cab47

Browse files
authored
872 solved. (#64)
1 parent 07efe17 commit 28cab47

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ continually updating 😃.
110110
* [257. Binary Tree Paths](src/0257_binary_tree_paths/binary_tree_paths.go)   *`binary tree`*
111111
* [404. Sum of Left Leaves](src/0404_sum_of_left_leaves/sum_of_left_leaves.go)   *`binary tree`*
112112
* [437. Path Sum III](src/0437_path_sum_3/path_sum_3.go)   *`binary tree`*
113+
* [872. Leaf-Similar Trees](src/0872_leaf_similar_trees/leaf_similar_trees.go)   *`binary tree`*
113114

114115
### Binary Search
115116
* [33. Search in Rotated Sorted Array](./src/0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)   *`array;`*  *`binary search`*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
872. Leaf-Similar Trees
3+
https://leetcode.com/problems/leaf-similar-trees/
4+
*/
5+
// time: 2019-01-08
6+
7+
package lst
8+
9+
import (
10+
"reflect"
11+
)
12+
13+
// TreeNode Definition for a binary tree node.
14+
type TreeNode struct {
15+
Val int
16+
Left *TreeNode
17+
Right *TreeNode
18+
}
19+
20+
// time complexity: O(n1 + n2), where n is nodes number in the tree.
21+
// space complexity: O(h1 + h2), where h is height of the tree.
22+
func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool {
23+
l1 := make([]int, 0)
24+
l2 := make([]int, 0)
25+
26+
dfs(root1, &l1)
27+
dfs(root2, &l2)
28+
29+
return reflect.DeepEqual(l1, l2)
30+
}
31+
32+
func dfs(root *TreeNode, l *[]int) {
33+
if nil == root {
34+
return
35+
}
36+
37+
if root.Left == nil && root.Right == nil {
38+
*l = append(*l, root.Val)
39+
}
40+
dfs(root.Left, l)
41+
dfs(root.Right, l)
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package lst
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func createBinaryTree(nums []int) *TreeNode {
9+
return performCreate(nums, 0)
10+
}
11+
12+
func performCreate(nums []int, index int) *TreeNode {
13+
if index >= len(nums) {
14+
return nil
15+
}
16+
17+
tree := TreeNode{Val: nums[index]}
18+
tree.Left = performCreate(nums, 2*index+1)
19+
tree.Right = performCreate(nums, 2*index+2)
20+
return &tree
21+
}
22+
23+
func TestLeafSimilar(t *testing.T) {
24+
type arg struct {
25+
root1, root2 *TreeNode
26+
}
27+
28+
testCases := []arg{
29+
{root1: createBinaryTree([]int{3, 5, 1, 6, 2, 9, 8}), root2: createBinaryTree([]int{6, 4, 1, 6, 2, 9, 8})},
30+
{root1: createBinaryTree([]int{3, 5, 1, 6, 2, 9, 4}), root2: createBinaryTree([]int{6, 4, 1, 6, 2, 9, 8})},
31+
}
32+
33+
expected := []bool{true, false}
34+
35+
for index, data := range testCases {
36+
if res := leafSimilar(data.root1, data.root2); !reflect.DeepEqual(res, expected[index]) {
37+
t.Errorf("expected %t, got %t", expected[index], res)
38+
}
39+
}
40+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,4 @@
101101
|0717|[717. 1-bit and 2-bit Characters](0717_1_bit_and_2_bit_characters/1bitand2bitc.go)|Easy||
102102
|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||
103103
|0747|[Largest Number At Least Twice of Others](./0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)|Easy||
104+
|0872|[872. Leaf-Similar Trees](0872_leaf_similar_trees/leaf_similar_trees.go)|Easy|*`binary tree`*|

0 commit comments

Comments
 (0)