📜  查找叶节点相连的特殊二叉树的高度

📅  最后修改于: 2022-05-13 01:57:16.117000             🧑  作者: Mango

查找叶节点相连的特殊二叉树的高度

给定一棵特殊的二叉树,它的叶子节点连接起来形成一个循环双向链表,求它的高度。

例如,

1 
       /   \ 
      2      3 
    /  \ 
   4    5
  /   
 6 

在上面的二叉树中,6、5和3是叶子节点,它们形成一个循环双向链表。这里,叶子节点的左指针将作为循环双向链表的前一个指针,其右指针将作为循环双向链表的下一个指针。

这个想法是遵循与我们查找正常二叉树高度类似的方法。我们递归地计算一个节点的左右子树的高度,并将高度分配给该节点作为两个孩子的高度的最大值加1。但是对于正常的二叉树,叶节点的左右孩子为空。但是,这里的叶子节点是一个循环双向链表节点。所以对于一个节点是叶子节点,我们检查节点的左边是否指向该节点,并且它的右边是否也指向该节点本身。

下面是上述想法的实现——

C++
// C++ program to calculate height of a special tree
// whose leaf nodes forms a circular doubly linked list
#include 
using namespace std;
 
// A binary tree Node
struct Node {
    int data;
    Node *left, *right;
};
 
// function to check if given node is a leaf node or node
bool isLeaf(Node* node)
{
    // If given node's left's right is pointing to given
    // node and its right's left is pointing to the node
    // itself then it's a leaf
    return node->left && node->left->right == node
           && node->right && node->right->left == node;
}
 
/* Compute the height of a tree -- the number of
Nodes along the longest path from the root node
down to the farthest leaf node.*/
int maxDepth(Node* node)
{
    // if node is NULL, return 0
    if (node == NULL)
        return 0;
 
    // if node is a leaf node, return 1
    if (isLeaf(node))
        return 1;
 
    // compute the depth of each subtree and take maximum
    return 1
           + max(maxDepth(node->left),
                 maxDepth(node->right));
}
 
// Helper function that allocates a new tree node
Node* newNode(int data)
{
    Node* node = new Node;
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return node;
}
 
// Driver code
int main()
{
    Node* root = newNode(1);
 
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->left->left->left = newNode(6);
 
    // Given tree contains 3 leaf nodes
    Node* L1 = root->left->left->left;
    Node* L2 = root->left->right;
    Node* L3 = root->right;
 
    // create circular doubly linked list out of
    // leaf nodes of the tree
 
    // set next pointer of linked list
    L1->right = L2, L2->right = L3, L3->right = L1;
 
    // set prev pointer of linked list
    L3->left = L2, L2->left = L1, L1->left = L3;
 
    // calculate height of the tree
    cout << "Height of tree is " << maxDepth(root);
 
    return 0;
}


Java
// Java implementation to calculate height of a special tree
// whose leaf nodes forms a circular doubly linked list
import java.io.*;
import java.util.*;
 
// User defined node class
class Node {
    int data;
    Node left, right;
    // Constructor to create a new tree node
    Node(int key)
    {
        data = key;
        left = right = null;
    }
}
 
class GFG {
 
    // function to check if given node is a leaf node or
    // node
    static boolean isLeaf(Node node)
    {
        // If given node's left's right is pointing to given
        // node and its right's left is pointing to the node
        // itself then it's a leaf
        return (node.left != null && node.left.right == node
                && node.right != null
                && node.right.left == node);
    }
    /* Compute the height of a tree -- the number of
    Nodes along the longest path from the root node
    down to the farthest leaf node.*/
    static int maxDepth(Node node)
    {
        // if node is NULL, return 0
        if (node == null)
            return 0;
 
        // if node is a leaf node, return 1
        if (isLeaf(node))
            return 1;
 
        // compute the depth of each subtree and take
        // maximum
        return 1
            + Math.max(maxDepth(node.left),
                       maxDepth(node.right));
    }
 
    // Driver code
    public static void main(String args[])
    {
        Node root = new Node(1);
 
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.left.left.left = new Node(6);
 
        // Given tree contains 3 leaf nodes
        Node L1 = root.left.left.left;
        Node L2 = root.left.right;
        Node L3 = root.right;
 
        // create circular doubly linked list out of
        // leaf nodes of the tree
 
        // set next pointer of linked list
        L1.right = L2;
        L2.right = L3;
        L3.right = L1;
 
        // set prev pointer of linked list
        L3.left = L2;
        L2.left = L1;
        L1.left = L3;
 
        // calculate height of the tree
        System.out.println("Height of tree is "
                           + maxDepth(root));
    }
}
// This code is contributed by rachana soma


Python3
""" program to Delete a Tree """
 
# Helper function that allocates a new
# node with the given data and None
# left and right pointers.
 
 
class newNode:
 
    # Construct to create a new node
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
 
# function to check if given node is a leaf node or node
 
 
def isLeaf(node):
 
    # If given node's left's right is pointing to given node
    # and its right's left is pointing to the node itself
    # then it's a leaf
    return node.left and node.left.right == node and \
        node.right and node.right.left == node
 
 
""" Compute the height of a tree -- the number of
Nodes along the longest path from the root node
down to the farthest leaf node."""
 
 
def maxDepth(node):
 
    # if node is None, return 0
    if (node == None):
        return 0
 
    # if node is a leaf node, return 1
    if (isLeaf(node)):
        return 1
 
    # compute the depth of each subtree and take maximum
    return 1 + max(maxDepth(node.left), maxDepth(node.right))
 
 
# Driver Code
if __name__ == '__main__':
    root = newNode(1)
 
    root.left = newNode(2)
    root.right = newNode(3)
    root.left.left = newNode(4)
    root.left.right = newNode(5)
    root.left.left.left = newNode(6)
 
    # Given tree contains 3 leaf nodes
    L1 = root.left.left.left
    L2 = root.left.right
    L3 = root.right
 
    # create circular doubly linked list out of
    # leaf nodes of the tree
 
    # set next pointer of linked list
    L1.right = L2
    L2.right = L3
    L3.right = L1
 
    # set prev pointer of linked list
    L3.left = L2
    L2.left = L1
    L1.left = L3
 
    # calculate height of the tree
    print("Height of tree is ", maxDepth(root))
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#
// C# implementation to calculate height of a special tree
// whose leaf nodes forms a circular doubly linked list
using System;
 
// User defined node class
public class Node {
    public int data;
    public Node left, right;
    // Constructor to create a new tree node
    public Node(int key)
    {
        data = key;
        left = right = null;
    }
}
 
public class GFG {
 
    // function to check if given node is a leaf node or
    // node
    static bool isLeaf(Node node)
    {
        // If given node's left's right is pointing to given
        // node and its right's left is pointing to the node
        // itself then it's a leaf
        return (node.left != null && node.left.right == node
                && node.right != null
                && node.right.left == node);
    }
    /* Compute the height of a tree -- the number of
    Nodes along the longest path from the root node
    down to the farthest leaf node.*/
    static int maxDepth(Node node)
    {
        // if node is NULL, return 0
        if (node == null)
            return 0;
 
        // if node is a leaf node, return 1
        if (isLeaf(node))
            return 1;
 
        // compute the depth of each subtree and take
        // maximum
        return 1
            + Math.Max(maxDepth(node.left),
                       maxDepth(node.right));
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        Node root = new Node(1);
 
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.left.left.left = new Node(6);
 
        // Given tree contains 3 leaf nodes
        Node L1 = root.left.left.left;
        Node L2 = root.left.right;
        Node L3 = root.right;
 
        // create circular doubly linked list out of
        // leaf nodes of the tree
 
        // set next pointer of linked list
        L1.right = L2;
        L2.right = L3;
        L3.right = L1;
 
        // set prev pointer of linked list
        L3.left = L2;
        L2.left = L1;
        L1.left = L3;
 
        // calculate height of the tree
        Console.WriteLine("Height of tree is "
                          + maxDepth(root));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

Height of tree is 4

&list=PLqM7alHXFySHCXD7r1J0ky9Zg_GBB1dbk