欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

树-相关的基础算法代码总结

程序员文章站 2024-03-22 17:51:04
...

主要罗列了下代码,看到代码后应该会清晰一些。

1.先序遍历(非递归)

	//先序遍历
	public void PreOrder(TreeNode root){
		if(null == root){
			return;
		}
		
		Stack<TreeNode> stk = new Stack<TreeNode>();
		
		while(root!=null || !stk.isEmpty()){
			
			while(root != null){
				System.out.println(root.val);
				stk.push(root);
				root = root.left;
			}
			
			if(!stk.isEmpty()){//栈不为空
				root = stk.pop();
				root = root.right;
			}
			
		}
		
		
	}

2.中序遍历(非递归)

	//中序遍历
	public void InOrder(TreeNode root){
		if(root == null){
			return;
		}
		
		Stack<TreeNode> stk = new Stack<TreeNode>();
		
		while(root != null || !stk.isEmpty()){
			
			while(root != null){
				stk.push(root);
				root = root.left;
			}
			
			if(!stk.isEmpty()){
				root = stk.pop();
				System.out.println(root.val);
				root = root.right;
			}
		}
		
	}

3.后序遍历(非递归)

	//后续遍历
	public void PostOrder(TreeNode root){
		
		if(null == root){
			return;
		}
		
		Stack<TreeNode> stk = new Stack<TreeNode>();
		Stack<TreeNode> stk1 = new Stack<TreeNode>();
		
		stk.push(root);
		
		while(!stk.isEmpty()){
			root = stk.pop();
			stk1.push(root);
			
			if(root.left != null){
				stk.push(root.left);
			}
			
			if(root.right != null){
				stk.push(root.right);
			}
		}//end while
		
		//输出
		while(!stk1.isEmpty()){
			System.out.println(stk1.pop().val);
		}
		
		
	}

4.层次遍历输出

	//层次遍历--队列
	public ArrayList<Integer> PrintFromTopToBottom(TreeNode root){
		
		ArrayList<Integer> resultList = new ArrayList<Integer>();//作为结果返回
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		TreeNode current;
		
		if(root == null){
			return resultList;
		}else{
			
			queue.offer(root);
			resultList.add(root.val);

			while(!queue.isEmpty()){
				current = queue.poll();
				if(current.left != null){
					queue.offer(current.left);
					resultList.add(current.left.val);
				}
				
				if(current.right != null){
					queue.offer(current.right);
					resultList.add(current.right.val);
				}
				
			}//end while--结束

		}
		
		return resultList;
	}

5.求树的深度

	//树的深度
	public int TreeDepth(TreeNode root){
		
		if(null == root){
			return 0;
		}
		
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		int cur,last;
		int level = 0;
		TreeNode current;
		
		queue.offer(root);
		while(!queue.isEmpty()){//队列不为空
			
			cur = 0;
			last = queue.size();
			
			while(cur<last){
				current = queue.poll();
				if(current.left != null){
					queue.offer(current.left);
				}
				
				if(current.right != null){
					queue.offer(current.right);
				}
				cur++;//这个必须得操作  不然是死循环
			}//end while
			
			level++;
		}
		
		return level;
	}

6.树的之字形打印

	//树的之字形打印
	public void ZigZagPrint(TreeNode root){
		
		if(null == root){
			return;
		}
		Stack<TreeNode> stk = new Stack<TreeNode>();
		Stack<TreeNode> stk1 = new Stack<TreeNode>();
		
		stk.push(root);
		while(!stk.isEmpty() || !stk1.isEmpty()){
			
			while(!stk.isEmpty()){
				TreeNode node = stk.pop();
				System.out.println(node.val);
				if(node.left != null){
					stk1.push(node.left);
				}
				if(node.right!=null){
					stk1.push(node.right);
				}
				
			}//end while
			
			while(!stk1.isEmpty()){
				TreeNode node = stk1.pop();
				System.out.println(node.val);
				if(node.right != null){
					stk.push(node.right);
				}
				
				if(node.left != null){
					stk.push(node.left);
				}
				
				
			}//end while
		}//end outer while
	}

 

相关标签: 树相关基础算法