94BinaryTreeInorderTraversal

题目:给一棵树,使用迭代的方法输出该树的中序遍历序列,left-root-right

思路:stack

   	 public List inorderTraversal(TreeNode root) {
	        ArrayList res = new ArrayList();
	        Stack st = new Stack();
	        TreeNode trNode = root, top; //reference
	        if(root == null) return res;
	        else st.push(root);
	        trNode = root.left;
	        while(!st.isEmpty() || trNode != null){
	        	 while(trNode != null){
	 	        	st.push(trNode);
	 	        	trNode = trNode.left;
	 	        }
	        	top = st.pop();
	        	res.add(top.val);
	        	trNode = top.right;
	        }
	        return res;
	    }

你可能感兴趣的:(leetcode)