TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void PostOrder =NULL) { PostOrder(root->left,vec); PostOrder(root->right,vec); } } vector<int> postorderTraversal(TreeNode *root) { vector<int>vec; PostOrder
原题链接: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 跟 Binary Tree Inorder Traversal
题目 Given an n-ary tree, return the postorder traversal of its nodes’ values. 再遍历根节点 解答 class Solution { // 存放结果 List<Integer> list = new ArrayList<>(); public List<Integer> postorder null) return list; // 递归遍历其他节点 for(Node node: root.children) postorder
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values
Given a binary tree, return the postorder traversal of its nodes' values.
Binary Tree Postorder Traversal Desicription Given a binary tree, return the postorder traversal of its
val = _val; children = _children; } }; */ class Solution { public List<Integer> postorder } }; */ class Solution { List<Integer> list = new ArrayList<>(); public List<Integer> postorder return list; } for (Node child : root.children) { postorder return list; } } Runtime: 1 ms, faster than 100.00% of Java online submissions for N-ary Tree Postorder Memory Usage: 48.2 MB, less than 40.66% of Java online submissions for N-ary Tree Postorder Traversal
问题描述 Given a binary tree, return the postorder traversal of its nodes’ values.
链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a , 0, postorder.length-1, inorderMap); } public TreeNode buildTree(int[] inorder, int iLeft, >iRight || pLeft>pRight) return null; TreeNode cur = new TreeNode(postorder[pRight , i+1, iRight, postorder, pLeft+i-iLeft, pRight-1, inorderMap); return cur; } }
PAT 1138.Postorder Traversal(25) Suppose that all the keys in a binary tree are distinct positive integers Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder 输出格式: For each test case, print in one line the first number of the postorder traversal sequence of (node* root){ if(root){ postorder(root->lchild); postorder(root->rchild); pre[i]);} for(int i=1; i<=n; ++i){scanf("%d", &mid[i]);} node* root = build(1, n, 1, n); postorder
题目: Given inorder and postorder traversal of a tree, construct the binary tree. ) { if (postorder.empty()) return nullptr; TreeNode *root = makeNode(inorder.begin (), inorder.end(), postorder.begin(), postorder.end()); return root; } }; Java参考代码: /** , postBegin + leftSize); root.right = makeNode(inorder, index + 1, inEnd, postorder, postBegin , postorder, 0, postorder.length); return root; } }
Question: Given inorder and postorder traversal of a tree, construct the binary tree. [postorder[post_offset]]; root->right = buildTreeInOrder(postorder, post_offset-1, inorder, in_offset, in_offset-i); root->left = buildTreeInOrder(postorder, post_offset-(in_offset-i)-1 , postorder.size()-1, inorder, inorder.size()-1, postorder.size()); } }; 参考推荐: Construct Binary Tree from Inorder and Postorder Traversal
版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/88992726
val = _val; children = _children; } }; */ class Solution { public: vector<int> postorder val = _val; children = _children; } }; */ class Solution { public: vector<int> postorder reverse(result.begin(), result.end()); } }; Reference https://leetcode.com/problems/n-ary-tree-postorder-traversal
Given inorder and postorder traversal of a tree, construct the binary tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree ) { if (null == postorder || postorder.length < 1) { return null; } return buildTree(inorder, 0, inorder.length - 1, postorder, postorder.length - 1); } public TreeNode buildTree(int[] inorder, int start, int end, int[] postorder, int idx) {
Given inorder and postorder traversal of a tree, construct the binary tree. +1,ie); return root; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder ) { return dfs(postorder,0,inorder.size()-1,inorder,0,inorder.size()-1); } };
此题来自leetcode https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree , return the postorder traversal of its nodes' values.
Construct Binary Tree from Inorder and Postorder Traversal Desicription Given inorder and postorder traversal private: TreeNode* generateTree(int postStart, int postEnd, int inStart, int inEnd, vector<int>& postorder int inIndex = 0; for(int i = inStart; i <= inEnd; i++) { if(inorder[i] == postorder , inorder); root->right = generateTree(postEnd-inEnd+inIndex, postEnd-1, inIndex+1, inEnd, postorder ) { return generateTree(0, postorder.size()-1, 0, inorder.size()-1, postorder, inorder);
NULL) {} * }; */ class Solution { public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder ) { int current = inorder.size() - 1; return build(postorder, inorder, current, 0, postorder.size () - 1); } private: TreeNode* build(vector<int>& postorder, vector<int>& inorder, int& current if(current == -1 || start > end) { return nullptr; } int value = postorder , inorder, current, index + 1, end); node->left = build(postorder, inorder, current, start, index
Problem Given inorder and postorder traversal of a tree, construct the binary tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree ): if not len(postorder): return None root = TreeNode(postorder[-1]) idx = inorder.index(root.val) root.left = self.buildTree(inorder[ : idx], postorder[ : idx]) root.right = self.buildTree(inorder[idx+1 : ], postorder[idx : -1]) return root