📅  最后修改于: 2020-10-15 06:39:59             🧑  作者: Mango
在此程序中,我们需要找出二叉树的最大高度。二叉树的高度可以定义为根与叶之间的节点数。最大高度将是根与最深叶之间的级别数。为了解决这个问题,我们遍历左子树并计算左子树的高度。同样,通过遍历右子树来计算其高度。最大高度将是左子树和右子树的高度中的最大值。
在上面的二叉树中,
左子树的高度为2。右子树的高度为4。MaxHeight = Max(leftHeight,rightHeight)+1;此处,1表示根节点的高度
给定二叉树的最大高度为(4 + 1)= 5,用白色虚线表示。
#Represent a node of binary tree
class Node:
def __init__(self,data):
#Assign data to the new node, set left and right children to None
self.data = data;
self.left = None;
self.right = None;
class BinaryTree:
def __init__(self):
#Represent the root of binary tree
self.root = None;
#findHeight() will determine the maximum height of the binary tree
def findHeight(self, temp):
#Check whether tree is empty
if(self.root == None):
print("Tree is empty");
return 0;
else:
leftHeight = 0;
rightHeight = 0;
#Calculate the height of left subtree
if(temp.left != None):
leftHeight = self.findHeight(temp.left);
#Calculate the height of right subtree
if(temp.right != None):
rightHeight = self.findHeight(temp.right);
#Compare height of left subtree and right subtree
#and store maximum of two in variable max
maximum = leftHeight if (leftHeight > rightHeight) else rightHeight;
#Calculate the total height of the tree by adding the height of the root
return (maximum + 1);
bt = BinaryTree();
#Add nodes to the binary tree
bt.root = Node(1);
bt.root.left = Node(2);
bt.root.right = Node(3);
bt.root.left.left = Node(4);
bt.root.right.left = Node(5);
bt.root.right.right = Node(6);
bt.root.right.right.right= Node(7);
bt.root.right.right.right.right = Node(8);
#Display the maximum height of the given binary tree
print("Maximum height of given binary tree: " + str(bt.findHeight(bt.root)));
输出:
Maximum height of given binary tree: 5
#include
#include
//Represent a node of binary tree
struct node{
int data;
struct node *left;
struct node *right;
};
//Represent the root of binary tree
struct node *root = NULL;
//createNode() will create a new node
struct node* createNode(int data){
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
//Assign data to newNode, set left and right children to NULL
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
//findHeight() will determine the maximum height of the binary tree
int findHeight(struct node *temp){
//Check whether tree is empty
if(root == NULL) {
printf("Tree is empty\n");
return 0;
}
else {
int leftHeight = 0, rightHeight = 0;
//Calculate the height of left subtree
if(temp->left != NULL)
leftHeight = findHeight(temp->left);
//Calculate the height of right subtree
if(temp->right != NULL)
rightHeight = findHeight(temp->right);
//Compare height of left subtree and right subtree
//and store maximum of two in variable max
int max = (leftHeight > rightHeight) ? leftHeight : rightHeight;
//Calculate the total height of tree by adding height of root
return (max + 1);
}
}
int main()
{
//Add nodes to the binary tree
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->right->left = createNode(5);
root->right->right = createNode(6);
root->right->right->right= createNode(7);
root->right->right->right->right = createNode(8);
//Display the maximum height of the given binary tree
printf("Maximum height of given binary tree: %d", findHeight(root));
return 0;
}
输出:
Maximum height of given binary tree: 5
public class BinaryTree {
//Represent the node of binary tree
public static class Node{
int data;
Node left;
Node right;
public Node(int data){
//Assign data to the new node, set left and right children to null
this.data = data;
this.left = null;
this.right = null;
}
}
//Represent the root of binary tree
public Node root;
public BinaryTree(){
root = null;
}
//findHeight() will determine the maximum height of the binary tree
public int findHeight(Node temp){
//Check whether tree is empty
if(root == null) {
System.out.println("Tree is empty");
return 0;
}
else {
int leftHeight = 0, rightHeight = 0;
//Calculate the height of left subtree
if(temp.left != null)
leftHeight = findHeight(temp.left);
//Calculate the height of right subtree
if(temp.right != null)
rightHeight = findHeight(temp.right);
//Compare height of left subtree and right subtree
//and store maximum of two in variable max
int max = (leftHeight > rightHeight) ? leftHeight : rightHeight;
//Calculate the total height of tree by adding height of root
return (max + 1);
}
}
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
//Add nodes to the binary tree
bt.root = new Node(1);
bt.root.left = new Node(2);
bt.root.right = new Node(3);
bt.root.left.left = new Node(4);
bt.root.right.left = new Node(5);
bt.root.right.right = new Node(6);
bt.root.right.right.right= new Node(7);
bt.root.right.right.right.right = new Node(8);
//Display the maximum height of the given binary tree
System.out.println("Maximum height of given binary tree: " + bt.findHeight(bt.root));
}
}
输出:
Maximum height of given binary tree: 5
using System;
namespace Tree
{
public class Program
{
//Represent a node of binary tree
public class Node{
public T data;
public Node left;
public Node right;
public Node(T data) {
//Assign data to the new node, set left and right children to null
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree where T : IComparable{
//Represent the root of binary tree
public Node root;
public static Boolean flag = false;
public BinaryTree(){
root = null;
}
//findHeight() will determine the maximum height of the binary tree
public int findHeight(Node temp){
//Check whether tree is empty
if(root == null) {
Console.WriteLine("Tree is empty");
return 0;
}
else {
int leftHeight = 0, rightHeight = 0;
//Calculate the height of left subtree
if(temp.left != null)
leftHeight = findHeight(temp.left);
//Calculate the height of right subtree
if(temp.right != null)
rightHeight = findHeight(temp.right);
//Compare height of left subtree and right subtree
//and store maximum of two in variable max
int max = (leftHeight > rightHeight) ? leftHeight : rightHeight;
//Calculate the total height of tree by adding height of root
return (max + 1);
}
}
}
public static void Main()
{
BinaryTree bt = new BinaryTree();
//Add nodes to the binary tree
bt.root = new Node(1);
bt.root.left = new Node(2);
bt.root.right = new Node(3);
bt.root.left.left = new Node(4);
bt.root.right.left = new Node(5);
bt.root.right.right = new Node(6);
bt.root.right.right.right= new Node(7);
bt.root.right.right.right.right = new Node(8);
//Display the maximum height of the given binary tree
Console.WriteLine("Maximum height of given binary tree: " + bt.findHeight(bt.root));
}
}
}
输出:
Maximum height of given binary tree: 5
data = $data;
$this->left = NULL;
$this->right = NULL;
}
}
class BinaryTree{
//Represent the root of binary tree
public $root;
function __construct(){
$this->root = NULL;
}
//findHeight() will determine the maximum height of the binary tree
function findHeight($temp){
//Check whether tree is empty
if($this->root == null) {
print "Tree is empty
";
return 0;
}
else {
$leftHeight = 0;
$rightHeight = 0;
//Calculate the height of left subtree
if($temp->left != NULL)
$leftHeight = $this->findHeight($temp->left);
//Calculate the height of right subtree
if($temp->right != NULL)
$rightHeight = $this->findHeight($temp->right);
//Compare height of left subtree and right subtree
//and store maximum of two in variable max
$max = ($leftHeight > $rightHeight) ? $leftHeight : $rightHeight;
//Calculate the total height of tree by adding height of root
return ($max + 1);
}
}
}
$bt = new BinaryTree();
//Add nodes to the binary tree
$bt->root = new Node(1);
$bt->root->left = new Node(2);
$bt->root->right = new Node(3);
$bt->root->left->left = new Node(4);
$bt->root->right->left = new Node(5);
$bt->root->right->right = new Node(6);
$bt->root->right->right->right= new Node(7);
$bt->root->right->right->right->right = new Node(8);
//Display the maximum height of the given binary tree
print "Maximum height of given binary tree: " . $bt->findHeight($bt->root);
?>
输出:
Maximum height of given binary tree: 5