Find the height of the binary tree, the number of leaf nodes, the number of the K-layer node, and the problem of ancestor node.

Source: Internet
Author: User

First, the height of the two-fork tree

Similar to the idea of merging sort. The height of the lowest node is first obtained, and then the height of the higher node is compared respectively. Finally, recursion to the root node to find the height of the root node.

Find the height of the binary tree int height () {return getheight (_root);} int getheight (node<t> *root) {int left = 0;int right = 0;if (root->_leftchild! = NULL) Left + = GetHeight (root->_ Leftchild); if (root->_rightchild! = NULL) Right + = GetHeight (root->_rightchild); return left >= right? (left + 1): (right + 1);}

Second, the number of leaf nodes to find

Respectively recursive left and right subtree, when the last occurrence of the node left and right children are empty, it is a leaf node, thereby adding an operation.

Find the number of leaf nodes int count_leaves () {return count_leaves (_root);} int count_leaves (node<t> *root) {int count = 0;if (root = nullptr) return count;if (root->_leftchild! = nullptr) {C Ount + = count_leaves (root->_leftchild);} if (root->_rightchild! = nullptr) {count + = count_leaves (root->_rightchild);} if (Root->_leftchild = = nullptr && Root->_rightchild = = nullptr) Count + = 1;return count;}

Third, the number of K-layer nodes

According to the thought, the number of nodes of the first k layer, that is, the number of sub-nodes of the K-1 layer node, and the K-1 layer node can be obtained by the k-2 layer.

Computes the number of K-layer nodes of the binary tree int count_node (int k) {return Count_node (_root, k);} int Count_node (node<t> *root, int k) {int count = 0;if (k = = 1) if (root = NULL) return count + = 1;if (k > 1) {if (R Oot->_leftchild! = nullptr) Count + = Count_node (Root->_leftchild, k-1); if (root->_rightchild! = nullptr) Count + = Count_node (Root->_rightchild, k-1);} return count;}

Iv. finding the nearest ancestor node

Nearest ancestor node: that node that reaches two nodes and is closest to two nodes. You can write a find function that determines whether a node has a binary tree, so that the last node is the closest ancestor node by traversing its left and right subtrees from the root node until it is unable to reach both nodes at the same time (the deepest recursion).

Find the nearest common ancestor node node<t>* findancestor (t x1, T x2) {return findancestor (_root, x1, x2);} node<t>* FindAncestor (node<t> *root, T x1, t x2) {if (root = nullptr) return nullptr;if (FindNode (Root, x1) &am p;& FindNode (Root, x2)) {if (FindAncestor (root->_leftchild, x1, x2)! = nullptr) root = Root->_leftchild;if ( FindAncestor (Root->_rightchild, x1, x2)! = nullptr) root = Root->_rightchild;return root;} return nullptr;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Find the height of the binary tree, the number of leaf nodes, the number of the K-layer node, and the problem of ancestor node.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.