经典题集之树二

  1. 求出二叉树的大小
  2. 编写算法按照从下到上的顺序来分层的遍历二叉树。
  3. 删除二叉树
  4. 求出二叉树的高度
  5. 寻找二叉树中最深的节点
  6. 确定二叉树中叶子节点的数量
  7. 使用非递归算法,确定二叉树中的满节点
  8. 使用非递归算法,确定二叉树中半节点的数量
  9. 判断两颗树的结构是否相同,即相同的位置都有节点,且值相等。
  10. 找出二叉树中各节点总和最大的那一
	//1.求出二叉树的大小
	public int BinaryTreeSizeRecursive(Node root) {
		if(root == null)
			return 0;
		else
			return BinaryTreeSizeRecursive(root.leftchild)+ 1 + BinaryTreeSizeRecursive(root.rightchild); 
	}
	public int BinaryTreeSizeNoRecursive(Node root) {
		if(root == null) return 0;
		int count = 0;
		Queue<Node> queue = new ArrayDeque<Node> ();
		queue.add(root);
		Node temp;
		while(!queue.isEmpty()) {
			temp = queue.poll();
			count++;
			if(temp.leftchild != null)
				queue.add(temp.leftchild);
			if(temp.rightchild != null)
				queue.add(temp.rightchild);
		}
		return count;
	}
	//2.编写算法按照从下到上的顺序来分层的遍历二叉树。
	public void Output_from_bottom_to_topBinaryTree(Node root) {
		if(root == null) return ;
		Queue<Node> queue = new ArrayDeque<Node>();
		Stack<Node> stack = new Stack<Node>();
		Node temp ;
		queue.add(root);
		while(!queue.isEmpty()) {
			temp = queue.poll();
			stack.push(temp);
			if(temp.rightchild != null)
				queue.add(temp.rightchild);
			if(temp.leftchild != null)
				queue.add(temp.leftchild);
		}
		while(!stack.isEmpty())
			System.out.println(stack.pop());	
	}
	//3.删除二叉树
	public void DeleteBinaryTree(Node root) {
		if(root == null)
			return ;
		DeleteBinaryTree(root.leftchild);
		DeleteBinaryTree(root.rightchild);
		root = null;
	}
	//4.求出二叉树的高度
	public int BinaryTreeLengthRecursive(Node root) {
		int leftChild,rightChild;
		if(root == null)
			return 0;
		else {
			leftChild = BinaryTreeLengthRecursive(root.leftchild);
			rightChild = BinaryTreeLengthRecursive(root.rightchild);
			if(leftChild > rightChild)
				return (leftChild + 1);
			else 
				return (rightChild + 1);
		}
	}
	public int BinaryTreeLengthNoRecursive(Node root) {
		int level = 0;
		Queue<Node> queue = new ArrayDeque<Node>();
		if(root == null)
			return 0;
		queue.add(root);
		queue.add(null);
		Node temp;
		while(!queue.isEmpty()) {
			temp = queue.poll();
			if(temp == null) {
				if(!queue.isEmpty()) //判断下一层是否有元素
					queue.add(null);
			}else {
				if(temp.leftchild != null)
					queue.add(temp.leftchild);
				if(temp.rightchild != null)
					queue.add(temp.rightchild);
			}
		}
		return level;
	}
	//5.寻找二叉树中最深的节点
	public Node FindDepthNode(Node root) {
		Queue<Node> queue = new ArrayDeque<Node>();
		Node temp = null;
		if(root == null)
			return null;
		queue.add(root);
		while(!queue.isEmpty()) {
			temp = queue.poll();
			if(temp.leftchild != null)
				queue.add(temp.leftchild);
			if(temp.rightchild != null)
				queue.add(temp.rightchild);
		}
		return temp;	
	}
	//6确定二叉树中叶子节点的数量
	public int leafCountOfBinaryTree(Node root) {
		Queue<Node> queue = new ArrayDeque<Node>();
		Node temp = null;
		int count = 0;
		if(root == null)
			return 0;
		queue.add(root);
		while(!queue.isEmpty()) {
			temp = queue.poll();
			if(temp.leftchild == null && temp.rightchild == null)
				count++;
			else {
				if(temp.leftchild != null)
					queue.add(temp.leftchild);
				if(temp.rightchild != null)
					queue.add(temp.rightchild);			
			}
		}
		return count;
	}
	//7.使用非递归算法,确定二叉树中的满节点
	public int FullNodeOfBinaryTree(Node root) {
		if(root == null) return 0;
		Queue<Node> queue = new ArrayDeque<Node> ();
		Node temp;
		int count = 0;
		queue.add(root);
		while(!queue.isEmpty()) {
			temp = queue.poll();
			if(temp.leftchild != null && temp.rightchild != null)
				count++;
			if(temp.leftchild != null)
				queue.add(temp.leftchild);
			if(temp.rightchild != null)
				queue.add(temp.rightchild);
		}
		return count;
	}
	//8.使用非递归算法,确定二叉树中半节点的数量
	public int HalfNodeOfBinaryTree(Node root) {
		Queue<Node> queue = new ArrayDeque<Node>();
		int count = 0;
		Node temp;
		queue.add(root);
		while(!queue.isEmpty()) {
			temp = queue.poll();
			if((temp.leftchild != null && temp.rightchild == null ) ||
					(temp.leftchild == null && temp.rightchild != null))
				count++;
			if(temp.leftchild != null)
				queue.add(temp.leftchild);
			if( temp.rightchild != null) 
				queue.add(temp.rightchild);
		}
		return count;
	}
	//9.判断两颗树的结构是否相同,即相同的位置都有节点,且值相等。
	public boolean IsSameConstrutor(Node root1,Node root2) {
		if(root1 == null && root2 == null)
			return true;
		if(root1 == null || root2 == null)
			return false;
		return 
				(root1.data == root2.data &&
				IsSameConstrutor(root1.leftchild, root2.leftchild)
				&& IsSameConstrutor(root1.rightchild, root2.rightchild));
	}
	//10.找出二叉树中各节点总和最大的那一层
	public int FindMaxLevelBinaryTree(Node root) {
		if(root == null) return 0;
		Node temp;
		int level = 0, maxLevel = 0;
		int currentSum = 0 ,maxSum = 0;
		Queue<Node> queue = new ArrayDeque<Node>();
		queue.add(root);
		queue.add(null);
		while(!queue.isEmpty()) {
			temp = queue.poll();
			if(temp == null) {
				if(currentSum > maxSum) {
					maxSum = currentSum;
					maxLevel = level;
				}
				currentSum = 0;
				if(!queue.isEmpty())
					queue.add(null);
				level++;
			}else {
				currentSum += temp.data;
				if(temp.leftchild != null)
					queue.add(temp.leftchild);
				if(temp.rightchild != null)
					queue.add(temp.rightchild);
			}
		}
		return maxLevel;
	}	

代码参考《程序员面试手册》

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值