LeetCode 题解(86): 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?

题解:

考察数据结构,一个set加一个堆栈解决问题。

c++:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        unordered_set<TreeNode*> visited;
        stack<TreeNode*> nodeStack;
        
        vector<int> result;
        if(root == NULL)
            return result;
        nodeStack.push(root);
        while(nodeStack.size() != 0) {
            TreeNode* current = nodeStack.top();
            if(current->right != NULL && visited.find(current->right) == visited.end())
                nodeStack.push(current->right);
            if(current->left != NULL && visited.find(current->left) == visited.end())
                nodeStack.push(current->left);
            if(current->right == NULL && current->left == NULL) {
                result.push_back(current->val);
                nodeStack.pop();
                visited.insert(current);
            }
            if((current->right != NULL && visited.find(current->right) != visited.end()) || (current->left != NULL && visited.find(current->left) != visited.end())) {
                result.push_back(current->val);
                nodeStack.pop();
                visited.insert(current);
            }
        }
        return result;
    }
    
};

Java版:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    public List<Integer> postorderTraversal(TreeNode root) {
        Set<TreeNode> visited = new HashSet<TreeNode>();
        List<Integer> result = new ArrayList<>();
        if(root == null)
            return result;
        Stack<TreeNode> nodeStack = new Stack<TreeNode>();
        nodeStack.push(root);
        while(nodeStack.empty() == false) {
            TreeNode current = nodeStack.peek();
            if(current.right != null && visited.contains(current.right) == false) {
                nodeStack.push(current.right);
            }
            if(current.left != null && visited.contains(current.left) == false) {
                nodeStack.push(current.left);
            }
            if(current.left == null && current.right == null) {
                nodeStack.pop();
                visited.add(current);
                result.add(current.val);
            }
            if((current.right != null && visited.contains(current.right) == true) || (current.left != null && visited.contains(current.left) == true)){
                nodeStack.pop();
                visited.add(current);
                result.add(current.val);
            }
        }
        return result;
    }
}

Python版:

class Solution:
    # @param {TreeNode} root
    # @return {integer[]}
    def postorderTraversal(self, root):
        result = []
        if root == None:
            return result
        visited = []
        nodeStack = []
        nodeStack.append(root)
        while len(nodeStack) != 0:
            current = nodeStack[-1]
            if current.right != None and not (current.right in visited):
                nodeStack.append(current.right)
            if current.left != None and not (current.left in visited):
                nodeStack.append(current.left)
            if current.left == None and current.right == None:
                nodeStack.pop()
                result.append(current.val)
                visited.append(current)
            if (current.right != None and (current.right in visited)) or (current.left != None and (current.left in visited)):
                nodeStack.pop()
                result.append(current.val)
                visited.append(current)
        return result


你可能感兴趣的:(Algorithm,LeetCode,面试题)