[LeetCode]Binary Tree Postorder Traversal

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

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

   1
    \
     2
    /
   3

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

recursive

java

public class Solution {
    ArrayList<Integer> result;
	public ArrayList<Integer> postorderTraversal(TreeNode root) {
        result = new ArrayList<Integer>();
        postTraversal(root);
        return result;
    }
	public void postTraversal(TreeNode root){
		if(root!=null){
			postTraversal(root.left);
			postTraversal(root.right);
			result.add(root.val);
		}
	}
}

c++

vector<int> postorderTraversal(TreeNode *root) {
    vector<int> result;
    postTraversal(root,result);
    return result;
}
void postTraversal(TreeNode *root, vector<int> &result){
    if(root!= NULL){
        postTraversal(root->left,result);
        postTraversal(root->right,result);
        result.push_back(root->val);
    }
}

iterative

java

public ArrayList<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> result1 = new ArrayList<Integer>();
		if(root == null) return result1;
		ArrayList<TreeNode> path = new ArrayList<TreeNode>();
		int index = 0;
		path.add(root);
		TreeNode head = root;
		while(index>=0){
			TreeNode topNode = path.get(index);
			if((topNode.left == null && topNode.right == null)||topNode.left ==head || topNode.right == head )
			{
				result1.add(topNode.val);
				path.remove(index);
				index--;
				head = topNode;
			}else {
				if(topNode.right!=null) {
					path.add(topNode.right);
					index++;
				}
				if(topNode.left!= null){
					path.add(topNode.left);
					index++;
				}
			}
		}
		return result1;
	}

refactor 

public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root==null) return result;
        Stack<TreeNode> path = new Stack<>();
        path.push(root);
        TreeNode head = root;
        while(!path.isEmpty()){
        	TreeNode topNode = path.peek();
        	if((topNode.right==null && topNode.left==null) || topNode.left==head || topNode.right==head){
        		result.add(topNode.val);
        		path.pop();
        		head = topNode;
        	}else {
				if(topNode.right!=null){
					path.push(topNode.right);
				}
				if(topNode.left!=null){
					path.push(topNode.left);
				}
			}
        }
        return result;
    }



c++

vector<int> postorderTraversal(TreeNode *root) {
        stack<TreeNode *> buffer;
    vector<int> output;
    if (root == NULL) return output;
    buffer.push(root);
    while (!buffer.empty()) {
        TreeNode *temp = buffer.top();
        if (temp->right == NULL && temp->left == NULL) {
            output.push_back(temp->val);
            buffer.pop();
            while (!buffer.empty() && (buffer.top()->left == temp || buffer.top()->right == temp)  ) {
                    temp = buffer.top();
                    output.push_back(temp->val);
                    buffer.pop();
                }
            } else {
                if (temp->right != NULL)
                    buffer.push(temp->right);
                if (temp->left != NULL)
                    buffer.push(temp->left);
            }
    }
    return output;
    }


你可能感兴趣的:(LeetCode,递归,二叉树,traversal)