二叉树笔试面试常见算法题

二叉树是笔试面试过程中常见的一种数据结构,而处理操作二叉树也是常见的算法题目,总结一下最近做的各种题目,记录一下自己之前写的相关基础算法,以后还会不断的增加自己遇到其他关于二叉树相关的算法题,为找工作做准备,感谢指正。

记录复习一下常用的二叉树相关的算法

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class outWay {
	public static void main(String[] args) {
		outWay ow = new outWay();
		int[] i = {4,8,9,6,1,3,2,7,0,5};
		TreeNode root = ow.buildTree(i);
		ow.findPath(root, 15);
		
		
	}
	//建立二叉树
	public TreeNode buildTree(int[] tree) {
		ArrayList treelist = new ArrayList();
		for(int i = 0; i pre) {
		if(root == null)	return;
		pre.add(root.val);
		preTree(root.left, pre);
		preTree(root.right, pre);
	}
	//中序遍历
	public void inTree(TreeNode root,ArrayList in) {
		if(root == null)	return;
		inTree(root.left, in);
		in.add(root.val);
		inTree(root.right, in);
	}
	//后序遍历
	public void postTree(TreeNode root,ArrayList post) {
		if(root == null)	return;
		postTree(root.left, post);
		postTree(root.right, post);
		post.add(root.val);
	}
	//从上往下打印二叉树
	public ArrayList PrintFromTopToBottom(TreeNode root) {
		ArrayList list = new ArrayList();
		if(root == null) return list;
		Queue queue = new LinkedList();
		queue.offer(root);
		while(!queue.isEmpty()) {
			TreeNode currentNode = queue.poll();
			if(currentNode.left!=null) queue.offer(currentNode.left);
			if(currentNode.right!=null) queue.offer(currentNode.right);
			list.add(currentNode.val);
		}
		return list;
	}
	//根据前序遍历和中序遍历,重建二叉树
	public TreeNode reBuildTreeByPreIn(int[] pre,int[] in) {
		 TreeNode root = usePreAndIn(pre,0,pre.length-1,in,0,in.length-1);
	     return root;
	}
	public TreeNode usePreAndIn(int[] pre,int preStart,int preEnd,int[] in,int inStart,int inEnd) {
		if(preStart > preEnd || inStart > inEnd	) return null;
		TreeNode root = new TreeNode(pre[preStart]);
		for(int i=inStart; i<=inEnd; i++) {
			if(in[i] == pre[preStart]) {
				root.left = usePreAndIn(pre, preStart + 1, preStart + (i-inStart), in, inStart, i-1);
				root.right = usePreAndIn(pre, preStart+(i-inStart)+1, preEnd, in, i+1, inEnd);
			}
		}
		return root;
	}
	//判断B树是否为A树的子结构
	public boolean HasSubTree(TreeNode root1,TreeNode root2) {
		if(root1 ==null || root2 ==null) return false;
		return isSubTree(root1,root2) || HasSubTree(root1.left,root2) ||HasSubTree(root1.right,root2);
	}
	public boolean isSubTree(TreeNode root1,TreeNode root2) {
		 if(root2 == null) return true;
	     if(root1 == null) return false;
	     if(root1.val == root2.val) {
	    	 return isSubTree(root1.left, root2.left) && isSubTree(root1.right, root2.right);
	     } else {
	    	 return false;
	     }
	}
	
	//打印出二叉树中结点值的和为输入整数的所有路径
	public ArrayList> findPath(TreeNode root,int target) {
		ArrayList> arraylist = new ArrayList>();
		ArrayList list = new ArrayList();
		getPath(root, target, list, arraylist);
		return arraylist;
	}
	public void getPath(TreeNode root, int target, ArrayList list, ArrayList> arraylist) {
		if(root == null) return ;
		list.add(root.val);
		target -= root.val;
		if(target < 0) {
			list.remove(list.size() - 1);
			return ;
		}
		if(target == 0 && root.left ==null && root.right == null) {
			arraylist.add(new ArrayList(list));
		}
		getPath(root.left,target,list,arraylist);
		getPath(root.right,target,list,arraylist);
		list.remove(list.size() - 1);
	}
	
}

你可能感兴趣的:(Java复习-并发,书山有路勤为径,二叉树,算法,Java)