📅  最后修改于: 2020-10-15 06:26:02             🧑  作者: Mango
在此程序中,我们需要检查给定二叉树的所有叶是否都处于同一级别。
如果没有任何子节点,则称节点为叶。在下图中,节点4、5和6是叶节点,因为它们没有任何子节点。节点4、5和6处于同一级别,即级别2。
#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 LeafLevel:
def __init__(self):
#Represent the root of binary tree
self.root = None;
#It will store level of first encountered leaf
self.level = 0;
#isSameLevel() will check whether all leaves of the binary tree is at same level or not
def isSameLevel(self, temp, currentLevel):
#Check whether tree is empty
if(self.root == None):
print("Tree is empty");
return true;
else:
#Checks whether node is None
if(temp == None):
return True;
if(temp.left == None and temp.right == None):
#If first leaf is encountered, set level to current level
if(self.level == 0):
self.level = currentLevel;
return True;
#Checks whether the other leaves are at the same level as that of the first leaf
else:
return (self.level == currentLevel);
#Checks for leaf node in left and right subtree recursively.
return (self.isSameLevel(temp.left, currentLevel + 1) and self.isSameLevel(temp.right, currentLevel + 1));
bt = LeafLevel();
#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);
#Checks whether all leaves of given binary tree is at same level
if(bt.isSameLevel(bt.root, 1)):
print("All leaves are at same level");
else:
print("All leaves are not at same level");
输出:
All leaves are at same level
#include
#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;
//It will store level of first encountered leaf
static int level = 0;
//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;
}
//isSameLevel() will check whether all leaves of the binary tree is at the same level or not
bool isSameLevel(struct node *temp, int currentLevel ) {
//Check whether tree is empty
if(root == NULL){
printf("Tree is empty\n");
return true;
}
else {
//Checks whether node is null
if(temp == NULL)
return true;
if(temp->left == NULL && temp->right == NULL) {
//If first leaf is encountered, set level to current level
if(level == 0) {
level = currentLevel ;
return true;
}
//Checks whether the other leaves are at the same level of that of first leaf
else
return (level == currentLevel) ;
}
//Checks for leaf node in left and right subtree recursively.
return (isSameLevel(temp->left, currentLevel + 1) && isSameLevel(temp->right, currentLevel + 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);
//Checks whether all leaves of given binary tree is at same level
if(isSameLevel(root, 1))
printf("All leaves are at same level");
else
printf("All leaves are not at same level");
return 0;
}
输出:
All leaves are at same level
public class LeafLevel {
//Represent a 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;
//It will store level of first encountered leaf
public static int level = 0;
public LeafLevel(){
root = null;
}
//isSameLevel() will check whether all leaves of the binary tree is at same level or not
public boolean isSameLevel(Node temp, int currentLevel ) {
//Check whether tree is empty
if(root == null){
System.out.println("Tree is empty");
return true;
}
else {
//Checks whether node is null
if(temp==null)
return true;
if(temp.left == null && temp.right == null) {
//If first leaf is encountered, set level to current level
if(level == 0) {
level = currentLevel ;
return true;
}
//Checks whether the other leaves are at same level of that of first leaf
else
return (level == currentLevel) ;
}
//Checks for leaf node in left and right subtree recursively.
return (isSameLevel(temp.left, currentLevel + 1) && isSameLevel(temp.right, currentLevel + 1)) ;
}
}
public static void main (String[] args) {
LeafLevel bt = new LeafLevel();
//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);
//Checks whether all leaves of given binary tree is at same level
if(bt.isSameLevel(bt.root, 1))
System.out.println("All leaves are at same level");
else
System.out.println("All leaves are not at same level");
}
}
输出:
All leaves are at same level
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 LeafLevel{
//Represent the root of binary tree
public Node root;
//It will store level of first encountered leaf
public static int level = 0;
public LeafLevel(){
root = null;
}
//isSameLevel() will check whether all leaves of the binary tree is at same level or not
public Boolean isSameLevel(Node temp, int currentLevel ) {
//Check whether tree is empty
if(root == null){
Console.WriteLine("Tree is empty");
return true;
}
else {
//Checks whether node is null
if(temp==null)
return true;
if(temp.left == null && temp.right == null) {
//If first leaf is encountered, set level to current level
if(level == 0) {
level = currentLevel ;
return true;
}
//Checks whether the other leaves are at same level of that of first leaf
else
return (level == currentLevel) ;
}
//Checks for leaf node in left and right subtree recursively.
return (isSameLevel(temp.left, currentLevel + 1) && isSameLevel(temp.right, currentLevel + 1)) ;
}
}
}
public static void Main()
{
LeafLevel bt = new LeafLevel();
//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);
//Checks whether all leaves of given binary tree is at same level
if(bt.isSameLevel(bt.root, 1))
Console.WriteLine("All leaves are at same level");
else
Console.WriteLine("All leaves are not at same level");
}
}
}
输出:
All leaves are at same level
data = $data;
$this->left = NULL;
$this->right = NULL;
}
}
class LeafLevel{
//Represent the root of binary tree
public $root;
//It will store level of first encountered leaf
public $level = 0;
function __construct(){
$this->root = NULL;
$this->level = 0;
}
//isSameLevel() will check whether all leaves of the binary tree is at same level or not
function isSameLevel($temp, $currentLevel ) {
//Check whether tree is empty
if($this->root == NULL){
print("Tree is empty
");
return true;
}
else {
//Checks whether node is null
if($temp == NULL)
return true;
if($temp->left == NULL && $temp->right == NULL) {
//If first leaf is encountered, set level to current level
if($this->level == 0) {
$this->level = $currentLevel ;
return true;
}
//Checks whether the other leaves are at same level of that of first leaf
else
return ($this->level == $currentLevel) ;
}
//Checks for leaf node in left and right subtree recursively.
return ($this->isSameLevel($temp->left, $currentLevel + 1) && $this->isSameLevel($temp->right, $currentLevel + 1)) ;
}
}
}
$bt = new LeafLevel();
//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);
//Checks whether all leaves of given binary tree is at same level
if($bt->isSameLevel($bt->root, 1))
print("All leaves are at same level");
else
print("All leaves are not at same level");
?>
输出:
All leaves are at same level