leetcode 虐我篇之(十六)Binary Tree Postorder Traversal
2014-08-25 14:46
309 查看
既然前面做完了二叉树的非递归前序、中序遍历,自然地就轮到这题了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? 同样地,这里用迭代的方法,同样用栈结构去存储。这里面我想了好一会儿,觉得用一个栈的话,感觉还不太好弄,如果有两个栈的话,就非常方便了。所以我定义了两个栈,其中一个是存最后结果的栈treeStack,另一个是用来做中间存储的,专门存储左结点的栈leftStack。现将根结点入栈treeStack,然后将左结点入栈leftStack,接着,把右结点当做根结点做刚才相同的入栈操作,如果右结点为NULL,则从左结点的栈中出栈,将其作为根结点,作相同的入栈工作。当所有的结点都入栈treeStack后,再将treeStack一个个出栈就好了。代码如下:
看了一下网上一些方法,好像要设置标志位,看是否是访问过的结点,感觉我这种方法感觉好理解一点啊。不过,花费的空间自然就多了。
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? 同样地,这里用迭代的方法,同样用栈结构去存储。这里面我想了好一会儿,觉得用一个栈的话,感觉还不太好弄,如果有两个栈的话,就非常方便了。所以我定义了两个栈,其中一个是存最后结果的栈treeStack,另一个是用来做中间存储的,专门存储左结点的栈leftStack。现将根结点入栈treeStack,然后将左结点入栈leftStack,接着,把右结点当做根结点做刚才相同的入栈操作,如果右结点为NULL,则从左结点的栈中出栈,将其作为根结点,作相同的入栈工作。当所有的结点都入栈treeStack后,再将treeStack一个个出栈就好了。代码如下:
std::vector<int> inorderTraversal(TreeN</span>ode *root) { std::vector<int> result; std::stack<TreeNode *> treeStack,leftStack; //two stack,leftStack is to save left child if (!root) { return result; } TreeNode *node = root; while(node || !leftStack.empty()) { treeStack.push(node); //root node push into the stack if (node->left) { leftStack.push(node->left); //left child push into the leftStack } // if (node->right) { node = node->right; } else { if (!leftStack.empty()) { node = leftStack.top(); leftStack.pop(); } else { node = NULL; } } } while(!treeStack.empty()) { node = treeStack.top(); result.push_back(node->val); treeStack.pop(); } return result; }
看了一下网上一些方法,好像要设置标志位,看是否是访问过的结点,感觉我这种方法感觉好理解一点啊。不过,花费的空间自然就多了。
相关文章推荐
- LeetCode Binary Tree Postorder Traversal
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal(用中序和后序建树,在题目给定的函数中完成) 【leetcode】
- LeetCode - 106 - Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode #106 Construct Binary Tree from Inorder and Postorder Traversal
- leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal
- LeetCode 105, 106. Construct Binary Tree from Preorder and Inorder/Inorder and Postorder Traversal
- 【leetcode】【106】Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] Construct binary tree from inorder and postorder traversal
- Construct Binary Tree from Inorder and Postorder Traversal - LeetCode 106
- [Leetcode] Binary Tree Postorder Traversal
- [LeetCode145]Binary Tree Postorder Traversal
- functionclass[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-Binary Tree Postorder Traversal
- LeetCode:Binary Tree Postorder Traversal(二叉树的后序遍历)
- [LeetCode] Binary Tree Postorder Traversal [递归版]
- leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- leetcode:Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal -- leetcode
- Leetcode-Binary Tree Postorder Traversal