经典题集之树三

  1. 将二叉树汇的根节点到每一个叶子结点的路径分别打印出来
  2. 判断树中有没有一条连接根节点与叶节点的路径,其中各节点的数据之和等于调用方法锁指定的总和。
  3. 求二叉树的所有元素之和
  4. 求树的镜像
  5. 判断两颗树是否为镜像
  6. 打印二叉树特定节点的所有祖先
  7. 曲折遍历
  8. 在二叉搜索树中查找元素
  9. 在二叉搜索树中寻找最小的元素
  10. 在二叉搜索树中寻找最大的元素  
  11. 从二叉树中删除元素
  12. //1.将二叉树汇的根节点到每一个叶子结点的路径分别打印出来
    	public void printfLeafToRootPath(Node root,int path[],int pathLen) {
    		if(root == null) return ;
    		path[pathLen] = root.data;
    		pathLen++;
    		if(root.leftchild == null && root.rightchild == null)
    			Print(path,pathLen);
    		else {
    			printfLeafToRootPath(root.leftchild, path, pathLen);
    			printfLeafToRootPath(root.rightchild, path, pathLen);
    		}
    	}
    	public void Print(int path[],int pathLen) {
    		for(int i=0;i<pathLen;i++)
    			System.out.println(path[i]);
    	}
    	//2.判断树中有没有一条连接根节点与叶节点的路径,其中各节点的数据之和等于调用方法锁指定的总和。
    	public boolean HasSumPath(Node root,int sum) {
    		if(root == null) 
    			return false;
    		else {
    			int remain = sum - root.data;
    			if((root.leftchild != null && root.rightchild != null) 
    				|| (root.leftchild == null && root.rightchild == null))
    				return
    						HasSumPath(root.leftchild, remain)
    						|| HasSumPath(root.rightchild, remain);
    			else if(root.leftchild != null)
    				return 
    						HasSumPath(root.leftchild, remain);
    			else
    				return 
    						HasSumPath(root.rightchild,remain);
    		}
    	}
    	//2.求二叉树的所有元素之和
    	public int SumOfBinaryTreeUseRecursive(Node root) {
    		if(root == null)
    			return 0;
    		else
    			return (root.data + SumOfBinaryTreeUseRecursive(root.leftchild) + SumOfBinaryTreeUseRecursive(root.rightchild));
    	}
    	
    	public int SumOfBinaryTreeNoUseRecursive(Node root) {
    		Node temp;
    		Queue<Node> queue = new ArrayDeque<Node>();
    		int sum = 0;
    		queue.add(root);
    		while(!queue.isEmpty()) {
    			temp = queue.poll();
    			sum += temp.data;
    			if(temp.leftchild != null)
    				queue.add(temp.leftchild);
    			if(temp.rightchild != null)
    				queue.add(temp.rightchild);
    		}
    		return sum;
    	}
    	//3.求树的镜像
    	public Node MirrorOfBinaryTree(Node root) {
    		Node temp;
    		if(root != null) {
    			MirrorOfBinaryTree(root.leftchild);
    			MirrorOfBinaryTree(root.rightchild);
    			temp = root.leftchild;
    			root.leftchild = root.rightchild;
    			root.rightchild = temp;
    		}
    		return root;
    	}
    	//4.判断两颗树是否为镜像
    	public boolean AreMirrorOfBinaryTree(Node root1,Node root2) {
    		if(root1 == null && root2 == null)
    			return true;
    		if(root1 == null || root2 == null)
    			return false;
    		if(root1.data != root2.data)
    			return false;
    		else
    			return
    					AreMirrorOfBinaryTree(root1.leftchild, root2.rightchild)
    				&&  
    					AreMirrorOfBinaryTree(root2.leftchild, root1.rightchild);
    	}
    		//5.打印二叉树特定节点的所有祖先
    	public boolean PrintAncestors(Node root,Node node) {
    		if(root == null)
    			return false;
    		if(root.leftchild == node || root.rightchild == node ||
    				PrintAncestors(root.leftchild, node) || PrintAncestors(root.rightchild, node)) {
    			System.out.println(root.data);
    			return true;
    		}
    		return false;
    	}
    	//6.曲折遍历
    	public void ZigZagTraversal(Node root) {
    		if(root == null) return ;
    		Node temp;
    		Stack<Node> tempStack ;
    		Stack<Node> current =new Stack<Node>();
    		Stack<Node> next = new Stack<Node>();
    		boolean leftToRight = true;
    		while(!current.isEmpty()) {
    			temp = current.pop();
    			if(temp != null) {
    				System.out.println(temp.data);
    				if(leftToRight) {
    					if(temp.leftchild != null)
    						next.push(temp.leftchild);
    					if(temp.rightchild != null)
    						next.push(temp.rightchild);
    				}else {
    					if(temp.rightchild != null)
    						next.push(temp.rightchild);
    					if(temp.leftchild != null)
    						next.push(temp.leftchild);
    				}
    			}
    			if(current.isEmpty()) {
    				leftToRight = false;
    				tempStack = current;
    				current = next;
    				next = tempStack;
    			}
    		}
    	}
    	//7.在二叉搜索树中查找元素
    	public Node FindElementUseRecursive(Node root,int data) {
    		if(root == null) return null;
    
    		if(data < root.data) return FindElementUseRecursive(root.leftchild,data);
    		else if( data > root.data) return FindElementUseRecursive(root.rightchild, data);
    		else return root;
    	}
    	public Node FindElementNoRecursive(Node root ,int data) {
    		if(root == null) return null;
    		while(root != null) {
    			if(root.data > data)
    				root = root.leftchild;
    			else if(root.data < data)
    				root = root.rightchild;
    			else
    				return root;
    		}
    		return null;
    	}
    	//8.在二叉搜索树中寻找最小的元素
    	public Node FindMinElementUseRecursive(Node root) {
    		if(root == null) 
    			return null;
    		else {
    			if(root.leftchild == null)
    				return root;
    			else 
    				return FindMinElementUseRecursive(root.leftchild);
    		}
    	}
    	public Node FindMinElementNoRecursive(Node root) {
    		if(root == null) return null;
    		while(root.leftchild != null) 
    			root = root.leftchild;
    		return root;
    	}
    	//9.在二叉搜索树中寻找最大的元素
    	public Node FindMaxElementUseRecursive(Node root) {
    		if(root == null)
    			return null;
    		else {
    			if(root.rightchild == null)
    				return root;
    			else 
    				return FindMaxElementUseRecursive(root.rightchild);
    		}	
    	}
    	public Node FindMaxElementNoRecursive(Node root) {
    		if(root == null)
    			return null;
    		while(root.rightchild != null)
    			root = root.rightchild;
    		return root;
    	}
    	//10.从二叉树中删除元素
    	public Node DeleteNodeFromBinaryTree(Node root,int data) {
    		Node temp;
    		if(root == null)
    			System.out.println("NO DATA");
    		else if(data < root.data)
    			root.leftchild = DeleteNodeFromBinaryTree(root.leftchild, data);
    		else if(data> root.data)
    			root.rightchild = DeleteNodeFromBinaryTree(root.rightchild, data);
    		else {
    			if(root.leftchild != null && root.rightchild != null) {
    				temp = FindMaxElementNoRecursive(root.leftchild);
    				root.data = temp.data;
    				root.leftchild = DeleteNodeFromBinaryTree(root.leftchild, data);
    			}else {
    				temp= root;
    				if(root.leftchild == null)
    					root = root.rightchild;
    				else if(root.rightchild == null)
    					root = root.leftchild;
    			}
    		}
    		return root;
    	}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值