> For the complete documentation index, see [llms.txt](https://wskdsgcf.gitbook.io/mastering-go-zh-cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wskdsgcf.gitbook.io/mastering-go-zh-cn/05.0/05.3.md).

# 05.3 Go 语言中的二叉树

**二叉树**是每个节点最多有两个分支的数据结构。“最多”表示一个节点可以连接一至两个子节点，或者不连接其他子节点。**树的根节点**是树结构中的第一个节点。**树的深度**，又称为树的高度，是从树的根节点到所有节点的路径中最长的一个。而节点的深度是该节点到树的根所经过的路径中边的数量。**叶节点**是没有子节点的节点。

若一个树的根节点到任意两个叶节点的距离之差不大于 1，则认为这个树是**平衡的**。顾名思义，非平衡树不是平衡的。树的平衡操作困难又缓慢，所以最好从一开始就让你的树保持平衡，而不是在建好树之后再去调整，尤其是这个树有很多节点的时候。

下图是一个非平衡二叉树。它的节根点是 J，叶节点包括 A、G、W 和 D。

![](/files/-LelHpCpz91hOGmmzUQT)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wskdsgcf.gitbook.io/mastering-go-zh-cn/05.0/05.3.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
