* @return: Postorder in ArrayList which contains node values. */vector<int>postorderTraversal(TreeNode * root){// write your code herevector<int> result; traverse(root, result);returnresult; }// 遍历法的递归函数,没有返回值,函数参数result贯穿整个递归过程用来记录遍历的结果voidtraverse(TreeNode ...
TreeNode *pNodeA1 = CreateBinaryTreeNode(8); TreeNode *pNodeA2 = CreateBinaryTreeNode(6); TreeNode *pNodeA3 = CreateBinaryTreeNode(1); TreeNode *pNodeA4 = CreateBinaryTreeNode(9); TreeNode *pNodeA5 = CreateBinaryTreeNode(2); TreeNode *pNodeA6 = CreateBinaryTreeNode(4); TreeNode *pNo...
Given the root of a binary tree, return the postorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [3,2,1] Example 2: Input: root = [] Output: [] Example 3: Input: root = [1] Output: [1] Constraints: The number of the nodes in the tr...
scala importscala.collection.mutable.{ListBuffer,Stack}objectLC145{defpostorderTraversal(root:TreeNode):List[Int]={valrs=ListBuffer.empty[Int]defdfs(node:TreeNode):Unit={if(node!=null){dfs(node.left)dfs(node.right)rs.append(node.value)}}dfs(root)rs.toList}defpostorderTraversalV2(root:TreeNode...
5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> postorderTraversal(TreeNode *root) { 13 if(root==NULL) ...
publicList<Integer>postorderTraversal(TreeNoderoot){List<Integer>list=newArrayList<>();Stack<TreeNode>stack=newStack<>();TreeNodecur=root;TreeNodelast=null;while(cur!=null||!stack.isEmpty()){if(cur!=null){stack.push(cur);cur=cur.left;}else{TreeNodetemp=stack.peek();//是否变到右子树if...
LeetCode Binary Tree Postorder Traversal 1.题目 Given a binary tree, return thepostordertraversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 1. 2. 3. 4. 5. return [3,2,1]....
public:vector<int>postorderTraversal(TreeNode*root){vector<int>result;stack<TreeNode*>st;TreeNode*p=root,*q=NULL;do{while(p)st.push(p),p=p->left;q=NULL;while(!st.empty()){p=st.top();st.pop();if(p->right==q)//右子树为空或者已经访问{result.push_back(p->val);q=p;}else/...
每天一算:Binary Tree Postorder Traversal 程序员吴师兄 + 关注 预计阅读时间2分钟 6 年前 LeetCode上第145 号问题:二叉树的后序遍历 题目 给定一个二叉树,返回它的 后序 遍历。 示例: 输入: [1,null,2,3] 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 用栈(Stack)...
A node's children are those nodes that appear immediately beneath the node itself. A node's parent is the node immediately above it. A tree's root is the single node that contains no parent.Figure 1 shows an example of the chain of command in a fictional company....