java实现树

面试题25:二叉树中和为某一值的路径

题目:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。
从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。二叉树节点的定义如下:
class BinaryTreeNode{
    String data;
    BinaryTreeNode left;
    BinaryTreeNode right;

}  

注:Stack为java实现栈中的Stack

public class FindPath_25 {
	
	public static void main(String []args) {
		BinaryTreeNode node1 = new BinaryTreeNode(10);
		BinaryTreeNode node2 = new BinaryTreeNode(5);
		BinaryTreeNode node3 = new BinaryTreeNode(12);
		BinaryTreeNode node4 = new BinaryTreeNode(4);
		BinaryTreeNode node5 = new BinaryTreeNode(7);
		
		node1.connection(node1, node2, node3);
		node1.connection(node2, node4, node5);
		
		findPath(node1,22);
	}
	
	
	private static void findPath(BinaryTreeNode root, int expectValue) {
		if(root==null) return;
		Stack<Integer> stack = new Stack<Integer>();
		int currentSum = 0;
		findPath(root,expectValue,stack,currentSum);
		//System.out.println(stack.empty());
	}

	private static void findPath(BinaryTreeNode root, int expectValue,
			Stack<Integer> stack, int currentSum) {
		currentSum += root.data;
		stack.push(root.data);
		
		//如果是叶子结点,并且路径上结点的和等于输入的值,打印出这条路径
	    boolean leaf = root.left == null && root.right == null;
	    if(currentSum==expectValue&&leaf) {
	    	for(int i=stack.storage.size()-1;i>=0;i--) {
	    		System.out.print(stack.storage.get(i)+" ");
	    	}
	    	System.out.println();
	    }
	    
	    //如果不是叶子结点,则遍历它的子节点
	    if(root.left!=null) findPath(root.left,expectValue,stack,currentSum);
	    if(root.right!=null) findPath(root.right,expectValue,stack,currentSum);
	    
	    stack.pop();
	}

}

class BinaryTreeNode{
    int data;
    BinaryTreeNode left;
    BinaryTreeNode right;
    
    BinaryTreeNode(int data) {
    	this.data = data;
    	left = null;
    	right = null;
    }
    
    public void connection(BinaryTreeNode root,BinaryTreeNode left,BinaryTreeNode right) {
    	if(root == null) return;
    	root.left = left;
    	root.right = right;
    }
} 

面试题24:二叉搜索树的后序遍历序列

题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。
如果是则返回true,否则返回false。假设输入数组的任意两个数字都互不相同。

public class SquenceOfBST {
	
	public static void main(String []args) {
		int[] squence = {5,7,6,9,11,10,8};
		System.out.println(VerifySquenceOfBST(squence,squence.length));
	}

	private static boolean VerifySquenceOfBST(int[] squence, int length) {
		if(squence==null||length<=0) return false;
		int root = squence[length-1];
		
		//在二叉树中左子树的节点小于根节点
		int i=0;
		for(;i<length-1;i++) {
			if(root<squence[i]) {
				break;
			}
		}
		//在二叉树中右子树的节点大于根节点
		int j = i;
		for(;j<length-1;j++) {
			if(root>squence[j]) {
				return false;
			} 
		}
		
		//求出左右子树
		int squenceLeft[] = new int[i];
		int squenceRight[] = new int[j-i];
		for(int z=0;z<i;z++) {
			squenceLeft[z] = squence[z];
		}
		for(int z=0;z<j-i;z++) {
			squenceRight[z] = squence[i+z];
		}
		
		//判断左子树是不是二叉搜索树
		boolean left = true;
		if(i>0) left = VerifySquenceOfBST(squenceLeft,squenceLeft.length);
		
		//判断右子树是不是二叉搜索树
		boolean right = true;
		if(j-i>0) right = VerifySquenceOfBST(squenceRight,squenceRight.length);
		
		return left&&right;
	}
	
}

面试题23:从上往下打印二叉树


题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印。

类BitreeMirrorImg在下面的二叉树的镜像中。

import java.util.LinkedList;

public class PrintFromTopToBottom_23 {

	public static void main(String[] args) {
		BitreeMirrorImg bm  = new BitreeMirrorImg();
		BinaryTreeNode root = null;
		root = bm.createTree(root);
		System.out.println(root);
		printFromToBottom(root);
	}

	public static void printFromToBottom(BinaryTreeNode root) {
		if(root==null) {
			return;
		}
		LinkedList<BinaryTreeNode> queue  = new LinkedList<BinaryTreeNode>();
		queue.add(root);
		while(queue.size()>0) {
			BinaryTreeNode bt = queue.poll();
			System.out.print(bt.data + " ");
			if(bt.left!=null) queue.add(bt.left);
			if(bt.right!=null) queue.add(bt.right);
		}
		
	}
	 
}

面试题19:二叉树的镜像


题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像

import java.util.Scanner;

//二叉树的结构
class BinaryTreeNode{
    String data;
    BinaryTreeNode left;
    BinaryTreeNode right;
}
/**
 *  输入一个二叉树,输出它的镜像
 */
public class BitreeMirrorImg {
    Scanner scanner = new Scanner(System.in);
    //建立二叉树
    public BinaryTreeNode createTree(BinaryTreeNode root){
        String data;
        data = scanner.next();
        if(data.equals("#")){
            return null;
        }
        root = new BinaryTreeNode();
        root.data = data;
        root.left = createTree(root.left);
        root.right = createTree(root.right);
        return root;
    }
    //得到二叉树的镜像
    public void mirror(BinaryTreeNode root){
        if(root == null){
            return;
        }
        if((root.left == null) && (root.right == null)){
            return;
        }
        BinaryTreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        mirror(root.left);
        mirror(root.right);
    }
    //层次遍历二叉树
    public void levelTraverse(BinaryTreeNode root){
        java.util.LinkedList<BinaryTreeNode> list= new java.util.LinkedList<BinaryTreeNode>();
        list.add(root);
        while(list.size() != 0){
            BinaryTreeNode node = list.removeFirst();
            System.out.print(node.data + " ");
            if(node.left != null){
                list.add(node.left);
            }
            if(node.right != null){
                list.add(node.right);
            }
        }
    }
    public static void main(String[] args) {
        BitreeMirrorImg bmi = new BitreeMirrorImg();
        BinaryTreeNode root = null;
        root = bmi.createTree(root);
        bmi.mirror(root);
        bmi.levelTraverse(root);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值