二叉树是笔试面试过程中常见的一种数据结构,而处理操作二叉树也是常见的算法题目,总结一下最近做的各种题目,记录一下自己之前写的相关基础算法,以后还会不断的增加自己遇到其他关于二叉树相关的算法题,为找工作做准备,感谢指正。
记录复习一下常用的二叉树相关的算法
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<TreeNode> treelist = new ArrayList<TreeNode>();
for(int i = 0; i<tree.length; i++) {
treelist.add(new TreeNode(tree[i]));
}
for(int i = 0; i < treelist.size()/2 -1; i++) {
treelist.get(i).left = treelist.get( 2*i + 1);
treelist.get(i).right = treelist.get( 2*i + 2);
}
TreeNode lastParaentNode = treelist.get(treelist.size()/2 -1);
lastParaentNode.left = treelist.get((treelist.size()/2 -1)*2 + 1);
if(treelist.size()%2 == 1) {
lastParaentNode.right = treelist.get((treelist.size()/2 -1)*2 + 2);
}
return treelist.get(0);
}
//前序遍历
public void preTree(TreeNode root,ArrayList<Integer> pre) {
if(root == null) return;
pre.add(root.val);
preTree(root.left, pre);
preTree(root.right, pre);
}
//中序遍历
public void inTree(TreeNode root,ArrayList<Integer> in) {
if(root == null) return;
inTree(root.left, in);
in.add(root.val);
inTree(root.right, in);
}
//后序遍历
public void postTree(TreeNode root,ArrayList<Integer> post) {
if(root == null) return;
postTree(root.left, post);
postTree(root.right, post);
post.add(root.val);
}
//从上往下打印二叉树
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(root == null) return list;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
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<ArrayList<Integer>> findPath(TreeNode root,int target) {
ArrayList<ArrayList<Integer>> arraylist = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> list = new ArrayList<Integer>();
getPath(root, target, list, arraylist);
return arraylist;
}
public void getPath(TreeNode root, int target, ArrayList<Integer> list, ArrayList<ArrayList<Integer>> 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<Integer>(list));
}
getPath(root.left,target,list,arraylist);
getPath(root.right,target,list,arraylist);
list.remove(list.size() - 1);
}
}