You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
1.1 KiB

## sametree
5 years ago
### Instructions
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Write a function, `IsSameTree`, that returns `bool`.
5 years ago
### Expected function
```go
type TreeNodeL struct {
Left *TreeNodeL
5 years ago
Val int
Right *TreeNodeL
5 years ago
}
func IsSameTree(p *TreeNodeL, q *TreeNodeL) bool {
5 years ago
}
```
Example 1:
5 years ago
Input:
1
/ \
2 3
4 years ago
[1,2,3]
5 years ago
1
/ \
2 3
[1,2,3]
5 years ago
Output: true
5 years ago
Input:
5 years ago
1
/
2
[1,2]
1
\
2
5 years ago
[1,null,2]
5 years ago
Output: false
5 years ago
Input:
4 years ago
```
5 years ago
1
/ \
2 1
[1,2,1]
1
/ \
1 2
[1,1,2]
```
Output: false
5 years ago
### Usage
Here is a possible program to test your function :
```go
package main
func main() {
4 years ago
t1 := NewRandTree()
t2 := NewRandTree()
5 years ago
4 years ago
fmt.Println(IsSameTree(t1, t2))
5 years ago
}
```
### Output
```console
$ go run .
5 years ago
true
$
5 years ago
```