leetCode:Binary Tree Postorder Traversal,Binary Tree Preorder Traversal--JAVA version

Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,2,3].

package javatest;

//----------Stack I just taken from Internet-----------
class Stack {        
    
	  TreeNode[] data;  
	    
	  int maxSize;    
	  //栈顶位置  
	  int top;        
	    
	  public Stack(int maxSize) {        
	      this.maxSize = maxSize;        
	      data = new TreeNode[maxSize];        
	      top = -1;        
	  }        
	     
	  /** 
	   * 获取堆栈长度 
	   * @return 堆栈长度 
	   */  
	  public int getSize()  
	  {  
	    return maxSize;  
	  }  
	    
	  /** 
	   * 返回栈中元素的个数 
	   * @return 栈中元素的个数 
	   */  
	  public int getElementCount()  
	  {  
	    return top;  
	  }  
	    
	  /** 
	   * 判断栈空 
	   * @return 栈空 
	   */  
	  public boolean isEmpty()  
	  {  
	    return top == -1;  
	  }  
	    
	  /** 
	   * 判断栈满 
	   * @return 栈满 
	   */  
	  public boolean isFull()  
	  {  
	    return top+1 == maxSize;  
	  }  
	      
	  public boolean push(TreeNode data) {        
	    if(isFull())   
	    {        
	        System.out.println("栈已满!");        
	        return false;        
	    }        
	    this.data[++top] = data;        
	    return true;        
	  }        
	          
	  /**     
	   * 从栈中取出数据     
	   * @return 取出的数据     
	   */        
	  public TreeNode pop() throws Exception{        
	    if(isEmpty())   
	    {        
	        throw new Exception("栈已空!");        
	    }        
	    return this.data[top--];        
	  }        
	    
	  /** 
	   * 返回栈顶元素 
	   * @return 
	   */  
	  public TreeNode peek()  
	  {  
	    return this.data[getElementCount()];    
	  }  
}


class TreeNode{
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x){//the constructor
		this.val=x;
	}
}

public class Solution {
	//-----------PreOrder recursive------------------
	public static void recursivePreTraverse(TreeNode root){
		if(root!=null){
			System.out.println(root.val);
			recursivePreTraverse(root.left);
			recursivePreTraverse(root.right);
		}
	}
	//-----------Poster order recursive--------------------
	public static void recursivePostTraverse(TreeNode root){
		if(root!=null){
			recursivePostTraverse(root.left);
			recursivePostTraverse(root.right);
			System.out.println(root.val);
		}return;
	}
	
	
	//--------------non-recursive Pre-Order Traverse-----------------
	public static void nonRecursivePreTraverse(TreeNode root,Stack stack) throws Exception{
		if(root!=null){
			System.out.println(root.val);
			if(root.right!=null) stack.push(root.right);
			if(root.left!=null) stack.push(root.left);
			while(!stack.isEmpty()){
				TreeNode temp=stack.pop();
				System.out.println(temp.val);
				if(temp.right!=null) stack.push(temp.right);
				if(temp.left!=null) stack.push(temp.left);
			}//while
		}//if
	}
	
	//--------------non-recursive Post-Order Traverse-----------------
	public static void nonRecursivePostTraverse(TreeNode root,Stack stack) throws Exception{
		if(root!=null){
			stack.push(root);//push the root into the stack
			TreeNode temp=root.left;
			TreeNode temppre=root.left;
			while(!stack.isEmpty()){
				while(temp!=null){//go down along the left 
					stack.push(temp);
					temp=temp.left;
				}
				temp=stack.peek();//查看最后一个节点
				if((temp.right==null)|| (temppre==temp.right) ){
					temppre=stack.pop();//弹出最后一个节点,交给temppre
					System.out.println(temp.val);
					temp=stack.peek();//得到上一个节点,没有弹出
					temp=temp.right;//下面将右节点在上面的while中压入栈
					while(temp==temppre){
						temp=stack.pop();
						temppre=temp;
						
						System.out.println(temp.val);
					}
				}
				else{//最后一个节点右节点不是空的
					temp=temp.right;//在上面的while继续入站
				}//if
			}//while				
		}//if
	}
	
	//---------------------no-recursive in-Order-Traverse-----------------------
	public static void nonRecursiveInOrderTraverse(TreeNode root) throws Exception{
		Stack stack=new Stack(100);
		if(root!=null){
			stack.push(root);
			TreeNode temp=root.left;
			while(!stack.isEmpty()){
				while(temp!=null){
					stack.push(temp);
					temp=temp.left;//一直向左,把节点压入栈
				}//while
				if(!stack.isEmpty()){
					temp=stack.pop();//弹出最后一个节点
					System.out.println(temp.val);
					temp=temp.right;
					if(temp!=null){//如果右节点不空,将右节点压入栈
						stack.push(temp);
						temp=temp.left;
					}
				}//if
			}//while
		}//if
	}
	
	
	
	
	public static void main(String[] args) throws Exception{
		TreeNode a=new TreeNode(1);
		TreeNode b=new TreeNode(2);
		TreeNode c=new TreeNode(3);
		TreeNode d=new TreeNode(4);
		a.left=null;a.right=b;
		b.left=c;b.right=d;
		c.left=null;c.right=null;
		d.left=null;d.right=null;
//		recursivePreTraverse(a); //the output should be 1 2 3
//		recursivePostTraverse(a);//the output should be 3 2 1
		
		Stack stack=new Stack(100);
//		nonRecursivePreTraverse(a,stack);// the output should be 1 2 3
		nonRecursivePostTraverse(a,stack);//the output should be 3 4 2 1 
//		nonRecursiveInOrderTraverse(a);//the output should be 1 3 2 4
	}
	
}

后序有问题,还没改


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值