Thực sự thì định nghĩa tree height (Data structure) là gì?

Như tiêu đề, tui khá là bối rối về nó. Tôi đã tìm kiếm đáp án ở nhiều nguồn khác nhau và nhận được 2 sự khác biệt về height của single root và do đó dẫn đến 2 kết quả khác nhau. Ví dụ như cây dưới đây:

Theo định nghĩa từ xlinux.nist.gov/dads/HTML/height.html tree height là 2 :

The maximum distance of any node from the root. If a tree has only one node (the root), the height is zero. The height of an empty tree is not defined.

Và định nghĩa từ xlinux.nist.gov/dads/HTML/height.html (trên geeksforgeeks dùng thuật toán theo hướng này) tree height là 3 :

The height of a node in a tree is the length of the longest path from that node downward to a leaf, counting both the start and end vertices of the path. The height of a leaf is 1. The height of a nonempty tree is the height of its root. For example, tree
http://www.cs.ecu.edu/~karl/2530/fall17/Notes/graphics/bst-9.gif
has height 3. (There are 3 equally long paths from the root to a leaf. One of them is (30 18 24).) The height of an empty tree is defined to be 0.

Vậy có phải là tree height phụ thuộc vào định nghĩa của mỗi người, ở đâu sài định nghĩa nào thì mình theo đó?

Leetcode đang dùng định nghĩa là cây 1 node có độ cao là 1.

Nói về đúng hay sai thì độ cao cây sẽ không có tính gốc, vì gốc cây nằm & bò dưới đất.

height(node) = \begin{cases} 0 & \text{, nếu là node lá}\\ 1 + max(\{height(n), \forall n \in children(node)\})&\text{, nếu không phải} \end{cases}
3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?