Simple and gracious java solution with time of 1 ms


  • 1
    J
    public List<Integer> postorderTraversal(TreeNode root) {
    		LinkedList<Integer> res = new LinkedList<Integer>();
    		LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
    		while (true) {
    			while(null != root){
    				res.addFirst(root.val);
    				stack.addLast(root);
    				root = root.right;
    			}
    			if(stack.isEmpty()){
    				break;
    			}
    			root = stack.removeLast();
    			root = root.left;
    		}
    		return res;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.