二叉树面试总结 算法 java

二叉树面试中相关算法,java实现:

package com.js;

import java.util.LinkedList;
import java.util.Stack;

public class BinaryTree {
	public class TreeRoot{
		int data;
		TreeRoot leftChild = null;
		TreeRoot rightChild = null;
		public TreeRoot(int data){
			this.data = data;
		}
	}
	
	/**
	 * 递归方式实现
	 * @param root
	 */
	private void preOrder(TreeRoot root){
		if(root != null){
			System.out.println(root.data);
			preOrder(root.leftChild);
			preOrder(root.rightChild);
		}
	}
	private void inOrder(TreeRoot root){
		if(root != null){
			inOrder(root.leftChild);
			System.out.println(root.data);
			inOrder(root.rightChild);
		}
	}
	private void postOrder(TreeRoot root){
		if(root != null){
			postOrder(root.leftChild);
			postOrder(root.rightChild);
			System.out.println(root.data);
		}
	}
	
	/**
	 * 非递归方式实现
	 */
	private void noRecPreOrder(TreeRoot root){
		Stack<TreeRoot> stack = new Stack(); 
		while(root != null || stack.size() > 0){
			while(root != null){
				System.out.println(root.data);
				stack.push(root);
				root = root.leftChild;
			}
			if(stack.size() > 0){
				root = stack.pop();
				root = root.rightChild;
			}
		}
	}
	
	private void noRecInOrder(TreeRoot root){
		Stack<TreeRoot> stack = new Stack();
		while(root != null || stack.size() >0){
			while(root != null){
				stack.push(root);
				root = root.leftChild;
			}
			if(stack.size() > 0){
				root = stack.pop();
				System.out.println(root.data);
				root = root.rightChild;
			}
		}
	}
	
	private void onRecPostOrder(TreeRoot root){
		Stack<TreeRoot> stack = new Stack();
		TreeRoot node = root;
		while(root != null){
			//先左子树遍历
			for(;root.leftChild != null;root = root.leftChild){
				stack.push(root);
			}
			//当前节点无右子树 或者 右子树已经输出时
			while(root != null && (root.rightChild == null || root.rightChild == node) ){
				System.out.println(root.data);
				node = root;
				if(stack.empty()){
					return;
				}
				root = stack.pop();
			}
			stack.push(root);
			root = root.rightChild;
		}
	}
	
	
	
	private void noRecPastOrder(TreeRoot root){
		Stack<TreeRoot> stack = new Stack();
		TreeRoot node = root;
		while(root != null){
			//遍历左子树 使他入栈
			for(;root != null;root = root.leftChild){
				stack.push(root);
			}
			//右子树是否等于空 或 右子树已经输出
			while(root != null && (root.rightChild ==null || root.rightChild == node)){
				System.out.println(root.data);
				node = root;
				if(stack.empty()){
					return;
				}
				root = stack.pop();
			}
			stack.push(root);
			root  = root.rightChild;
		}
	}
	
	/**
	 * 求叶子节点个数
	 * @param root
	 * @return
	 */
	private int getNodeNum(TreeRoot root){
		if(root == null){
			return 0;
		}else{
			return (getNodeNum(root.leftChild) + 
					getNodeNum(root.rightChild)) +1;
		}
	}
	
	/**
	 * 求二叉树深度
	 */
	private int getDepth(TreeRoot root){
		if(root == null){
			return 0;
		}else{
			int leftDepth = getDepth(root.leftChild);
			int rightDepth = getDepth(root.rightChild);
			return (leftDepth > rightDepth ? leftDepth:rightDepth)+1;
		}
	}
	
	
	/*
	 * 按层遍历二叉树
	 */
	private void levelTree(TreeRoot root){
		LinkedList<TreeRoot> linkList = new LinkedList<>();
		linkList.push(root);
		while(!linkList.isEmpty()){
			TreeRoot cur = linkList.removeFirst();
			System.out.println(cur.data);
			if(cur.leftChild != null){
				linkList.add(cur.leftChild);
			}
			if(cur.rightChild != null){
				linkList.add(cur.rightChild);
			}
		}
	}
	
	/*
	 * 求二叉树第K层节点个数
	 */
	private int getKNum(TreeRoot root ,int k){
		if(root == null || k < 1){
			return 0;
		}
		if(k == 1){
			return 1;
		}
		int leftNum = getKNum(root,k-1);
		int rightNum = getKNum(root, k-1);
		return leftNum+rightNum;
	}
	
	/**
	 * 判断两个二叉树是否相同
	 */
	public boolean isSameRec(TreeRoot r1,TreeRoot r2){
		if(r1 == null && r2 == null){
			return true;
		}else if(r1 == null || r2 == null){
			return false;
		}
		if(r1.data == r2.data){
			return true;
		}
		boolean leftSame = isSameRec(r1.leftChild,r2.leftChild);
		boolean rightSame = isSameRec(r1.rightChild, r2.rightChild);
		return leftSame && rightSame;
	}
	
	/**
	 * 检验二叉树是否是平衡二叉树
	 */
	public boolean isAVLRec(TreeRoot root){
		if(root == null){
			return true;
		}
		if(Math.abs(getDepth(root.leftChild) - getDepth(root.rightChild)) > 1){
			return false;
		}
		return isAVLRec(root.leftChild) && isAVLRec(root.rightChild);
	}
}

    原文作者:蒋帅Android
    原文地址: https://blog.csdn.net/j18874964028sss/article/details/78379118
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞