java 二叉树第n层节点数及层序遍历

层序遍历简化代码


import java.util.LinkedList;

public class TreeBi {
	
	TreeBi leftTree;
	TreeBi rightTree;
	int data;
	
	public TreeBi(int data){
		this.data=data;
	}

	public static void main(String[] args) {


      	//               1
		//         2            3
		//            4       5
		// TODO Auto-generated method stub
		TreeBi root1 = new TreeBi(1);
		TreeBi root2 = new TreeBi(2);
		TreeBi root3 = new TreeBi(3);
		TreeBi root4 = new TreeBi(4);
		TreeBi root5 = new TreeBi(5);
		
		root2.rightTree=root4;
		root3.leftTree=root5;
		root1.leftTree=root2;
		root1.rightTree=root3;
		levelPrint(root1);
	}
	
	public static void levelPrint(TreeBi root){
		
		if(root == null){
			return;
		}
		LinkedList list = new LinkedList<>();
		list.add(root);
		while( ! list.isEmpty() ){
			TreeBi node = list.pop();
			System.out.println(node.data);
			if(node.leftTree!=null){
				list.add(node.leftTree);
			}
			if(node.rightTree!=null){
				list.add(node.rightTree);
			}
		}
		
	}

}

采用递归的思想进行求解

import java.util.LinkedList;

public class BTreeTest {

  public static void main(String[] args) {

    // 构造一棵二叉树 开始
    Tree rootTree = new Tree();
    rootTree.value =  1;

    Tree left = new Tree();
    left.value=2;
    Tree right = new Tree();
    right.value=3;

    Tree leftChildTree = new Tree();
    leftChildTree.value=4;
    Tree rightChildTree = new Tree();
    rightChildTree.value=5;

    Tree rightChileTreeLeft = new Tree();
    rightChileTreeLeft.value= 6;

    Tree rightChileTreeRight = new Tree();
    rightChileTreeRight.value= 7;

    left.leftTree=leftChildTree;
    left.rightTree=rightChildTree;
    right.leftTree=rightChileTreeLeft;
    right.rightTree=rightChileTreeRight;
    rootTree.leftTree=left;
    rootTree.rightTree=right;
    // 构造一棵二叉树 结束

     //            构造成如下树结构
     //
     //                1
     //          2            3
     //       4    5        6     7
     //
     //


    System.out.println("节点个数为:" + getTreeCount(rootTree,3));
    System.out.println("层序遍历结果为:");
    levelOrder(rootTree);
  }


  /**
   * 求树的第n层有多少个节点
   * @param root
   * @param n
   * @return
   */
  public static int getTreeCount(Tree root,int n){
      if(n <=0 || root == null){
        return 0;
      }
      if(n == 1) return 1;
      System.out.println(root.value);
      int left = getTreeCount(root.leftTree,n-1);
      int right = getTreeCount(root.rightTree,n-1);
      return left + right;
  }

  /**
   * 层序遍历
   * @param root
   */
   static void levelOrder(Tree root){
    Tree node;
    LinkedList list = new LinkedList();
    list.add(root);
    while (! list.isEmpty()){
      node = list.poll();
      System.out.println(node.value);;
      if(node.leftTree != null){
        list.offer(node.leftTree);
      }
      if(node.rightTree != null){
        list.offer(node.rightTree);
      }
    }
  }


}


class Tree{
   public Tree leftTree;
   public Tree rightTree;
   public int value;
}

运行结果截图

java 二叉树第n层节点数及层序遍历_第1张图片

 

你可能感兴趣的:(Java,常见面试知识点汇总,JAVA学习笔记,数据结构)