LeetCode 문제 풀이 (86): 바 이 너 리 트 리 우편번호 트 래 버 설

제목:
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

좋은 웹페이지 즐겨찾기