# treenode

package
v1.0.16 Latest Latest

Go to latest
Published: Dec 8, 2021 License: MIT

## Documentation ¶

### Overview ¶

Package treenode provides a set of tools for writing LeetCode problems locally.

### Constants ¶

This section is empty.

### Variables ¶

This section is empty.

### Functions ¶

#### func ToSlice ¶ added in v1.0.16

`func ToSlice(root *TreeNode) []nilint.NilInt`

Convert the TreeNode into a slice.

For example, we have the root of a tree with type TreeNode, we can make a convertion like below:

```ToSlice(root)
```

The output will be a slice. As is shown in all LeetCode problems, such as https://leetcode.com/problems/maximum-depth-of-binary-tree/.

### Types ¶

#### type TreeNode ¶

```type TreeNode struct {
Val   int
Left  *TreeNode
Right *TreeNode
}```

The TreeNode in this package can be used to declare the one in you package:

```type TreeNode = treenode.TreeNode // type alias

func foo(root *TreeNode) {}
```

#### func FromSlice ¶ added in v1.0.13

`func FromSlice(s []nilint.NilInt) *TreeNode`

Create a TreeNode from a slice.

For example, a root is provided as a abstract slice which contains level scan result of a binary tree, refers to the problem Maximum Depth of Binary Tree: https://leetcode.com/problems/maximum-depth-of-binary-tree/.

We can follow the code below to create a TreeNode of root:

```root := treenode.FromSlice([]nilint.NilInt{
nilint.NewInt(3),
nilint.NewInt(9),
nilint.NewInt(20),
nilint.NewNil(),
nilint.NewNil(),
nilint.NewInt(15),
nilint.NewInt(7),
})
```

Explanation:

We cannot use []int as the type of the slice root because it contains null. We define a type nilint.NilInt to represent it, and now we can define the slice as type []nilint.NilInt. We provide function NewInt and NewNil to create integer and null value.

The function treenode.NewOperator returns an instance of type treenode.Operator with a lot of features. But in our case, we only need to get the root TreeNode we need from the method Root.