数据结构编程: 统计二叉树中叶子结点的个数。

论坛 期权论坛 期权     
匿名   2018-4-28 02:21   4971   3
利用java的知识进行编程:统计二叉树中叶子结点的个数。

请哪位高手指教一下  再次先谢啦!
分享到 :
0 人收藏

3 个回复

倒序浏览
2#
子房sky  1级新秀 | 2018-4-30 01:12:51
叶子节点:没有孩子节点的节点
也就是说,当我们明白了叶子节点的定义后,只需要遍历一遍二叉树,把符合这种条件(左孩子节点和右孩子节点都为NULL的节点)的节点统计出来就可以了。
于是,实际上这个问题也就转化成了如何遍历二叉树?很显然,遍历二叉树是可以有多种方式的,如:前序遍历(递归/非递归)、中序遍历(递归/非递归)、后序遍历(递归/非递归)、层次遍历等等。
下面我将给出使用递归前序遍历以及层次遍历两种思路实现的求解叶子节点的示例代码吧,仅供参考。其他几种实现方式思路类似,可自行尝试。示例代码如下:
package聽cn.zifangsky.tree.questions;

import聽org.junit.Test;

import聽cn.zifangsky.queue.LinkQueue;
import聽cn.zifangsky.tree.BinaryTreeNode;

/**
聽*聽求二叉树中叶子节点的个数
聽*聽@author聽Administrator
聽*
聽*/
public聽class聽Question2聽{

/**
聽*聽通过递归前序遍历获取叶子节点个数
聽*聽@param聽root
聽*聽@return
聽*/
public聽int聽getNumberOfLeavesByPreOrder(BinaryTreeNode聽root){
  if(root聽==聽null){
   return聽0;
  }else{
   if(root.getLeft()聽==聽null聽&&聽root.getRight()聽==聽null){聽//叶子节点
    return聽1;
   }else{
    return聽getNumberOfLeavesByPreOrder(root.getLeft())聽+聽getNumberOfLeavesByPreOrder(root.getRight());
   }
  }

}


/**
聽*聽使用层次遍历获取二叉树叶子节点个数
聽*聽
聽*聽@时间复杂度聽O(n)
聽*聽@param聽root
聽*/
public聽int聽getNumberOfLeavesByQueue(BinaryTreeNode聽root){
  int聽count聽=聽0;聽//叶子节点总数
  LinkQueue聽queue聽=聽new聽LinkQueue();
  if(root聽!=聽null){
   queue.enQueue(root);
  }

  while聽(!queue.isEmpty())聽{
   BinaryTreeNode聽temp聽=聽(BinaryTreeNode)聽queue.deQueue();
   //叶子节点:左孩子节点和右孩子节点都为NULL的节点
   if(temp.getLeft()聽==聽null聽&&聽temp.getRight()聽==聽null){
    count++;
   }else{
    if(temp.getLeft()聽!=聽null){
     queue.enQueue(temp.getLeft());
    }
    if(temp.getRight()聽!=聽null){
     queue.enQueue(temp.getRight());
    }
   }
  }
  return聽count;
}


/**
聽*聽测试用例
聽*/
@Test
public聽void聽testMethods(){
  /**
  聽*聽使用队列构造一个供测试使用的二叉树
  聽*聽聽聽聽聽1
  聽*聽聽聽2聽聽聽聽3
  聽*聽4聽聽5聽聽6聽聽7
  聽*聽聽聽8聽9聽聽
  聽*/
  LinkQueue聽queue聽=聽new聽LinkQueue();
  BinaryTreeNode聽root聽=聽new聽BinaryTreeNode(1);聽//根节点

  queue.enQueue(root);
  BinaryTreeNode聽temp聽=聽null;
  for(int聽i=2;i聽rearNode;聽//队尾节点

public聽LinkQueue()聽{
  frontNode聽=聽null;
  rearNode聽=聽null;
}

/**
聽*聽返回队列是否为空
聽*聽@时间复杂度聽O(1)
聽*聽@return
聽*/
public聽boolean聽isEmpty(){
  return聽(frontNode聽==聽null);
}

/**
聽*聽返回存储在队列的元素个数
聽*聽@时间复杂度聽O(n)
聽*聽@return
聽*/
public聽int聽size(){
  int聽length聽=聽0;
  SinglyNode聽currentNode聽=聽frontNode;
  while聽(currentNode聽!=聽null)聽{
   length++;
   currentNode聽=聽currentNode.getNext();
  }

  return聽length;
}

/**
聽*聽入队:在链表表尾插入数据
聽*聽@时间复杂度聽O(1)
聽*聽@param聽data
聽*/
public聽void聽enQueue(K聽data){
  SinglyNode聽newNode聽=聽new聽SinglyNode(data);

  if(rearNode聽!=聽null){
   rearNode.setNext(newNode);
  }

  rearNode聽=聽newNode;

  if(frontNode聽==聽null){
   frontNode聽=聽rearNode;
  }
}

/**
聽*聽出队:删除表头节点
聽*聽@时间复杂度聽O(1)
聽*聽@return
聽*/
public聽Object聽deQueue(){
  if(isEmpty()){
   throw聽new聽RuntimeException("Queue聽Empty!");
  }else{
   Object聽result聽=聽frontNode.getData();

   if(frontNode聽==聽rearNode){
    frontNode聽=聽null;
    rearNode聽=聽null;
   }else{
    frontNode聽=聽frontNode.getNext();
   }

   return聽result;
  }
}

}单链表节点SinglyNode的定义:
package聽cn.zifangsky.linkedlist;

/**
聽*聽单链表的定义
聽*聽@author聽zifangsky
聽*聽@param聽
聽*/
public聽class聽SinglyNode聽{
private聽K聽data;聽//聽数据
private聽SinglyNode聽next;聽//聽该节点的下个节点

public聽SinglyNode(K聽data)聽{
  this.data聽=聽data;
}

public聽SinglyNode(K聽data,聽SinglyNode聽next)聽{
  this.data聽=聽data;
  this.next聽=聽next;
}

public聽K聽getData()聽{
  return聽data;
}

public聽void聽setData(K聽data)聽{
  this.data聽=聽data;
}

public聽SinglyNode聽getNext()聽{
  return聽next;
}

public聽void聽setNext(SinglyNode聽next)聽{
  this.next聽=聽next;
}

@Override
public聽String聽toString()聽{
  return聽"SinglyNode聽[data="聽+聽data聽+聽"]";
}

}
3#
热心网友  15级至尊 | 2018-4-30 01:12:52
public void getLeaves(){
  System.out.print("叶子节点为:");
  getLeaves(root);

  }

private void getLeaves(BinaryNode p){
  if(p!=null)
  {
   if(p.left==null&&p.right==null){
    System.out.print(p.data.toString()+"");
    t++;

   }
   getLeaves(p.left);
   getLeaves(p.right);
  }
}
// 构造一棵二叉树
public void getBinaryTree(String[] nodes) {
  this.root = (BinaryNode) getBinaryTreeByPreOrder(nodes);
}
private int i = 0;
private BinaryNode getBinaryTreeByPreOrder(String[] nodes) {
  BinaryNode node = null;
  if (i < nodes.length) {
   String tmp = nodes;
   i++;
   if (tmp != null) {
    node = new BinaryNode(tmp);
    // System.out.println("头结点"+node);
    node.left = (BinaryNode) getBinaryTreeByPreOrder(nodes);
    //System.out.println("左孩子" + node.left);
    node.right = (BinaryNode) getBinaryTreeByPreOrder(nodes);
    //System.out.println("右孩子" + node.right);
   }
  }
  return node;
}
public static void main(String[] args) {
  String[] nodes = { "A", "B", "D", null, "G",null,null,null, "C","E", null,null, "F", "H",null,
    null, null };
  BinaryTree bt = new BinaryTree();
  bt.getBinaryTree(nodes);
bt.getLeaves();
4#
热心网友  15级至尊 | 2018-4-30 01:12:53
用二叉树遍历算法就可以做到,叶子节点的意思就是没有子节点就表示是叶子节点。

二叉树遍历方便一点可以用递归算法做到。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:58388
帖子:6688
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP