[leetcode 145] Binary Tree Postorder Traversal (待完善)
来源:互联网 发布:2017淘宝收藏有礼 编辑:程序博客网 时间:2024/06/04 18:52
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?
递归
/** * 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> res; vector<int> postorderTraversal(TreeNode *root) { if (root == NULL) { return res; } postorderTraversal(root->left); postorderTraversal(root->right); res.push_back(root->val); return res; }};
栈
/** * 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> res; stack<int> nodeState; stack<TreeNode*> node; if (root == NULL) { return res; } node.push(root); nodeState.push(0); while (!node.empty()) { auto n = node.top(); node.pop(); auto state = nodeState.top(); nodeState.pop(); if (state == 0) { node.push(n); nodeState.push(1); if (n->right) { node.push(n->right); nodeState.push(0); } if (n->left) { node.push(n->left); nodeState.push(0); } } else { res.push_back(n->val); } } return res; }};
Morris遍历
基本思想和先序和中序遍历一样,但是要麻烦一些。只是在遍历前,增加了一个类似头节点的节点作为整个遍历过程的起始节点。由于后序遍历先访问左子树,然后是右子树,最后访问根节点,所以在Morris算法遍历过程中,增加一个头节点dump表示新二叉树的根节点,让这个新的根节点的左指针指向原二叉树的根节点,右指针为NULL。对于某个节点N,先遍历N的左子树的左子树,再逆序遍历从N节点的左子树的根节点到中序遍历过程访问的最右节点这条路径上的节点。
/** * 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> res; vector<int> postorderTraversal(TreeNode *root) { if (root == NULL) { return res; } TreeNode *dummy = new TreeNode(-1); dummy->left = root; TreeNode *p = dummy; while (p) { if (!p->left) { p = p->right; } else { auto tmp = p->left; while (tmp->right && tmp->right != p) { tmp = tmp->right; } if (!tmp->right) { tmp->right = p; p = p->left; } else { pustres(p->left, tmp); tmp->right = NULL; p = p->right; } } } return res; } //同反转单链表 void reverse(TreeNode *begin, TreeNode *end) { if (begin == end) { return ; } TreeNode *prev = begin; TreeNode *cur = begin->right; TreeNode *next = cur?cur->right:NULL; while (prev != end) { cur->right = prev; prev = cur; cur = next; next = next?next->right:NULL; } } void pustres(TreeNode *begin, TreeNode *end) { TreeNode *p; reverse(begin, end); p = end; while (true) { res.push_back(p->val); if (p == begin) { break; } p = p->right; } reverse(end, begin); }};
0 0
- [leetcode 145] Binary Tree Postorder Traversal (待完善)
- LeetCode(145)Binary Tree Postorder Traversal
- LeetCode: Binary Tree Postorder Traversal [145]
- LeetCode 145 Binary Tree Postorder Traversal
- LeetCode 145 Binary Tree Postorder Traversal
- leetcode || 145、Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal - LeetCode 145
- Leetcode[145]-Binary Tree Postorder Traversal
- LeetCode 145 Binary Tree Postorder Traversal
- leetcode(145):Binary Tree Postorder Traversal
- leetcode-145-Binary Tree Postorder Traversal
- [Leetcode 145, Hard] Binary Tree Postorder Traversal
- leetcode[145]:Binary Tree Postorder Traversal
- [leetcode-145]Binary Tree Postorder Traversal(c++)
- Leetcode #145 Binary Tree Postorder Traversal
- LeetCode 145 : Binary Tree Postorder Traversal
- LeetCode 145: Binary Tree Postorder Traversal
- 【LEETCODE】145-Binary Tree Postorder Traversal
- C++非递归遍历删除树节点——广度优先,逐层删除
- 推荐!国外程序员整理的机器学习资源大全
- 剑指offer 面试题40 数组中出现一次的两个数
- 设置eclipse显示行号
- AT 指令和常见错误码
- [leetcode 145] Binary Tree Postorder Traversal (待完善)
- vc++ slider 控件学习
- windows平台使用putty工具集中pscp上传文件夹
- 【ruby学习笔记】Hello World
- HDOJ 题目1595 find the longest of the shortest(枚举,最短路记录路径)
- Android UI-自定义日历控件
- 杂七杂八 - 新年
- dlopen、dlsym和dlclose的使用
- ssh连接服务器出现Agent admitted failure to sign using the key错误