二叉树的后序遍历 Binary Tree Postorder Traversal
2014-03-09 17:58
393 查看
递归方法:
迭代方法:
先让根节点进栈。谁出栈则要遍历谁。那么出栈入栈规则如下:
1、如果当前栈顶有左孩子且刚刚没访问过,则栈顶不出栈,且其左孩子入栈。
2、如果当前栈顶没左孩子或左孩子刚访问过,但有右孩子,则栈顶不出栈,且其右孩子入栈。
3、如果当前栈顶左右孩子都没有,则栈顶出栈。
4、如果当前栈顶有右孩子而且是刚刚访问过的,则栈顶出栈。
5、如果当前栈顶没有右孩子,而左孩子是刚刚访问过的,则栈顶出栈。
代码:
class Solution { public: vector<int> postorderTraversal(TreeNode *root) { vector<int> list; if(root == NULL) return list; postorder(root, list); return list; } void postorder(TreeNode *root, vector<int> &list) { if(root->left != NULL) postorder(root->left, list); if(root->right != NULL) postorder(root->right, list); list.push_back(root->val); } };
迭代方法:
先让根节点进栈。谁出栈则要遍历谁。那么出栈入栈规则如下:
1、如果当前栈顶有左孩子且刚刚没访问过,则栈顶不出栈,且其左孩子入栈。
2、如果当前栈顶没左孩子或左孩子刚访问过,但有右孩子,则栈顶不出栈,且其右孩子入栈。
3、如果当前栈顶左右孩子都没有,则栈顶出栈。
4、如果当前栈顶有右孩子而且是刚刚访问过的,则栈顶出栈。
5、如果当前栈顶没有右孩子,而左孩子是刚刚访问过的,则栈顶出栈。
代码:
/** * Definition for binary tree * 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) { vector<int> list; if(root == NULL) return list; stack<TreeNode *> s; s.push(root); TreeNode *pre = NULL; while(!s.empty()) { TreeNode *now = s.top(); if((now->left == NULL && now->right == NULL) || ((pre!=NULL)&&(now->right==pre)) || ((pre!=NULL)&&(now->right==NULL)&&(now->left==pre))) { list.push_back(now->val); pre = now; s.pop(); continue; } if(now->left != NULL && now->left != pre) { s.push(now->left); continue; } if(now->right != NULL) { s.push(now->right); continue; } } return list; } };
相关文章推荐
- LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal(从中序以及后序遍历结果中构造二叉树)
- Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal 中序后序遍历重组二叉树
- LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)
- LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium
- LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- Binary Tree Postorder Traversal 二叉树的后序遍历,使用堆栈,非递归
- LeetCode Binary Tree Postorder Traversal(二叉树的后序遍历 非递归实现)
- lintcode 容易题:Binary Tree Postorder Traversal 二叉树的后序遍历
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)
- LeetCode 145 Binary Tree Postorder Traversal (后序遍历二叉树)
- 【LeetCode】Binary Tree Postorder Traversal 二叉树的后序遍历 - Easy
- 【遍历二叉树】03二叉树的后序遍历【Binary Tree Postorder Traversal】
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- 【LeetCode笔记】Construct Binary Tree from Inorder and Postorder Traversal 中序、后序遍历求二叉树
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树