Java与算法(10)

Java与算法(10)

1.判断二叉树是否为平衡二叉树
public class IsBanlanceTree {
	public static class Node {
		public int value;
		public Node left;
		public Node right;

		public Node(int data) {
			this.value = data;
		}
	}

	public static boolean isBalance(Node head) {
		boolean[] res = new boolean[1];
		res[0] = true;
		getHeight(head, 1, res);
		return res[0];
	}

	public static int getHeight(Node head, int level, boolean[] res) {
		if (head == null) {
			return level;
		}
		int lH = getHeight(head.left, level + 1, res);
		if (!res[0]) {
			return level;
		}
		int rH = getHeight(head.right, level + 1, res);
		if (!res[0]) {
			return level;
		}
		if (Math.abs(lH - rH) > 1) {
			res[0] = false;
		}
		return Math.max(lH, rH);
	}
	public static void main(String[] args) {
		Node head = new Node(1);
		head.left = new Node(2);
		head.right = new Node(3);
		head.left.left = new Node(4);
		head.left.right = new Node(5);
		head.right.left = new Node(6);
		head.right.right = new Node(7);

		System.out.println(isBalance(head));
	}

}
2.找出二叉树搜索树中的两个错误节点
public class GetTwoErrorNode {
	public static class Node {
		int data;
		Node left;
		Node right;
		public Node(int data) {
			// TODO Auto-generated constructor stub
			this.data = data;
			left = null;
			right = null;
		}
	}
	
	public Node[] getError(Node root) {
		Node[] errors = new Node[2];
		Stack<Node> stack = new Stack<>();
		Node pre = null;
		while (!stack.isEmpty() || root!=null) {
			if (root!=null) {
				stack.push(root);
				root = root.left;
			} else {
				root = stack.pop();
				if (pre!=null && pre.data>root.data) {
					errors[0] = errors[0]==null?pre:errors[0];
					errors[1] = root;
				}
				pre = root;
				root = root.right;
			}
		}
			return errors;
	}
	
	
	
	
	
	public static void main(String[] args) {
		Node head1 = new Node(7);
		head1.left = new Node(3);
		head1.right = new Node(5);
		head1.left.left = new Node(2);
		head1.left.right = new Node(4);
		head1.right.left = new Node(6);
		head1.right.right = new Node(8);
		head1.left.left.left = new Node(1);
		GetTwoErrorNode errorNode = new GetTwoErrorNode();
		Node[] nodes = errorNode.getError(head1);
		for(int i=0;i<nodes.length;i++) {
			System.out.println(nodes[i].data);
		}
	}

}
3.二叉树按层打印和ZigZag打印
public class SpringByLevel {

	public static class Node {
		int data;
		Node left;
		Node right;
		public Node(int data) {
			// TODO Auto-generated constructor stub
			this.data = data;
			left = null;
			right = null;
		}
	}
	public static ArrayList<Node> trees = new ArrayList<>();
	public void buildTree(int[] array) {
		for(int i=0;i<array.length;i++) {
		trees.add(new Node(array[i]));
		}
		
		for(int index = 0;index<array.length/2-1;index++) {
			trees.get(index).left =  trees.get(index*2+1);
			trees.get(index).right = trees.get(index*2+2);
		}
		int lastindex = array.length/2-1;
		trees.get(lastindex).left = trees.get(lastindex*2+1);
		if (array.length%2==1) {
			trees.get(lastindex).right = trees.get(lastindex*2+2);
			
		}
		
	}
	
	public void printLevel(Node root) {
		Node last = root;
		Node nlast = null;
		Queue<Node> queue = new LinkedList<>();
		queue.offer(root);
		int level =1;
		System.out.print("Level  "+ level++ +":");
		while (!queue.isEmpty()) {
			root=queue.poll();
			System.out.print(root.data+" ");
			if (root.left!=null) {
				queue.offer(root.left);
				nlast = root.left;
			}
			if (root.right!=null) {
				queue.offer(root.right);
				nlast = root.right;
			}
			if (root==last && !queue.isEmpty()) {
				System.out.print("\nLevel  "+level++ +":");
				last = nlast;
			}
			
		}
		
	}
	
	public void ZigZag(Node root) {
		Deque<Node> deque =  new LinkedList<>();
		Node last = root;
		Node nlast = null;
		boolean lr = true;
		int level = 1;
		deque.offerFirst(root);
		prinfLorR(level, lr);
		while (!deque.isEmpty()) {
			if (lr) {
				root = deque.pollFirst();
				if (root.left!=null) {
					nlast = nlast==null?root.left:nlast;
					deque.offerLast(root.left);
				}
				if (root.right!=null) {
					nlast = nlast == null?root.right:nlast;
					deque.offerLast(root.right);
				}
				
				
				
			} else {
				root = deque.pollLast();
				if (root.right!=null) {
					nlast = nlast==null?root.right:nlast;
					deque.offerFirst(root.right);
				}
				if (root.left!=null) {
					nlast = nlast==null?root.left:nlast;
					deque.offerFirst(root.left);
				}
				
				
			}
			
			System.out.print(root.data+" ");
			if (root==last&&!deque.isEmpty()) {
				lr = !lr;
				last=nlast;
				nlast=null;
				System.out.println();
				prinfLorR(level++, lr);
				
			}
			
		}
		
		
		
		
	}
	
	public void prinfLorR(int level,boolean lr) {
		System.out.print("Level "+level +":");
		System.out.print(lr?" left to right":"right to left");
	}
	
	public static void main(String[] args) {
			int[] a = {1,2,3,4,5,6,7,8,9};
			SpringByLevel level = new SpringByLevel();
			level.buildTree(a);
			level.printLevel(trees.get(0));
			System.out.println("-------");
			level.ZigZag(trees.get(0));
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值