[C++/Python] Preoder and Postorder DFS
Explanation\nPre-order dfs the tree (node, left, right),\nupdate res[i] to the max height before node i in preorder,\ncan cover all nodes on the left of node i 
# **Explanation**\nPre-order dfs the tree (node, left, right),\nupdate `res[i]` to the max height before node `i` in preorder,\ncan cover all nodes on the left of node `i` in the tree.\n\nPost-order dfs the tree (node, right, left),\nupdate `res[i]` to the max height before node `i` in postorder,\ncan cover all nodes on the right of node `i` in the tree.\n<br>\n\n# **Complexity**\nTime `O(n)`\nSpace `O(n)`\n<br>\n\n**C++**\n```cpp\n    int preh[100001], posth[100001], maxh;\n    vector<int> treeQueries(TreeNode* root, vector<int>& queries) {\n        maxh = 0; pre(root, 0);\n        maxh = 0; post(root, 0);\n        vector<int> res;\n        for (int q : queries)\n            res.push_back(max(preh[q], posth[q]));\n        return res;\n    }\n\n    void pre(TreeNode* root, int h) {\n        if (!root) return;\n        preh[root->val] = maxh;\n        maxh = max(maxh, h);\n        pre(root->left, h + 1);\n        pre(root->right, h + 1);\n    }\n\n    void post(TreeNode* root, int h) {\n        if (!root) return;\n        posth[root->val] = maxh;\n        maxh = max(maxh, h);\n        post(root->right, h + 1);\n        post(root->left, h + 1);\n    }\n```\n**Shorter C++**\n```cpp\n    int seen[100001], maxh;\n    vector<int> treeQueries(TreeNode* root, vector<int>& queries) {\n        maxh = 0; dfs(root, 0);\n        maxh = 0; dfs(root, 0);\n        vector<int> res;\n        for (int q : queries)\n            res.push_back(seen[q]);\n        return res;\n    }\n\n    void dfs(TreeNode* root, int h) {\n        if (!root) return;\n        seen[root->val] = max(seen[root->val], maxh);\n        maxh = max(maxh, h);\n        dfs(root->left, h + 1);\n        dfs(root->right, h + 1);\n        swap(root->right, root->left);\n    }\n```\n\n**Python**\n```py\n    def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]:\n        res = defaultdict(int)\n\n        def dfs(root, h, maxh):\n            if not root: return maxh\n            res[root.val] = max(res[root.val], maxh)\n            root.left, root.right = root.right, root.left\n            return dfs(root.right, h + 1, dfs(root.left, h + 1, max(maxh, h)))\n\n        dfs(root, 0, 0)\n        dfs(root, 0, 0)\n        return [res[q] for q in queries]\n```\n
[]