Stack和non-recursion中序遍歷

package Stack;

import java.util.Stack;

public class Try {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Stack <Integer >stack = new Stack ();
		stack.push(9);
		System.out.println(stack.peek()); //peek是用的print 準備要pop的數但不用從數組中除掉
		System.out.println(stack.contains(9));
	}

}
	public void nonre_preorder_iterator(Node node){
		if(node == null) {   //如果node為空 則return nothing
			return;
		}
		//step 1
		Stack st = new Stack();
		//step 2
		st.push(node);
		
		while (st.empty() != true) { //當stack不是空的時侯進行以下循環
		//step 3
		Node curr = (Node) st.pop(); //cast st.pop() 的數據轉化為 Node type
		System.out.println(curr.id);
		if (curr.rightchild != null) {
			st.push(curr.rightchild);
		}
		if (curr.leftchild != null) {
			st.push(curr.leftchild);
		}
		}
		
	}

猜你喜欢

转载自blog.csdn.net/vincentgoodturtle/article/details/94615239
今日推荐