[LeetCode] Binary Tree Postorder Traversal

简介: This is a fundamental and yet classic problem. I share my three solutions here: Iterative solution using stack --- O(n) time and O(n) space; Recurs...

This is a fundamental and yet classic problem. I share my three solutions here:

  1. Iterative solution using stack --- O(n) time and O(n) space;
  2. Recursive solution --- O(n) time and O(n) space (considering the costs of call stack);
  3. Morris traversal --- O(n) time and O(1) space!!!

Iterative solution using stack:

 1 vector<int> postorderTraversal(TreeNode* root) {
 2     vector<int> nodes;
 3     stack<TreeNode*> toVisit;
 4     TreeNode* curNode = root;
 5     TreeNode* lastNode = NULL;
 6     while (curNode || !toVisit.empty()) {
 7         if (curNode) {
 8             toVisit.push(curNode);
 9             curNode = curNode -> left;
10         }
11         else {
12             TreeNode* topNode = toVisit.top();
13             if (topNode -> right && lastNode != topNode -> right)
14                 curNode = topNode -> right;
15             else {
16                 nodes.push_back(topNode -> val);
17                 lastNode = topNode;
18                 toVisit.pop();
19             }
20         }
21     }
22     return nodes;
23 }

Recursive solution:

 1 void postorder(TreeNode* root, vector<int>& nodes) {
 2     if (!root) return; 
 3     postorder(root -> left, nodes);
 4     postorder(root -> right, nodes);
 5     nodes.push_back(root -> val);
 6 }
 7 vector<int> postorderTraversal(TreeNode* root) {
 8     vector<int> nodes;
 9     postorder(root, nodes);
10     return nodes;
11 } 

Morris traversal:

 1 void reverseNodes(TreeNode* start, TreeNode* end) {
 2     if (start == end) return;
 3     TreeNode* x = start;
 4     TreeNode* y = start -> right;
 5     TreeNode* z;
 6     while (x != end) {
 7         z = y -> right;
 8         y -> right = x;
 9         x = y;
10         y = z;
11     }
12 }
13 void reverseAddNodes(TreeNode* start, TreeNode* end, vector<int>& nodes) {
14     reverseNodes(start, end);
15     TreeNode* node = end;
16     while (true) {
17         nodes.push_back(node -> val);
18         if (node == start) break;
19         node = node -> right;
20     }
21     reverseNodes(end, start);
22 }
23 vector<int> postorderTraversal(TreeNode* root) {
24     vector<int> nodes;
25     TreeNode* dump = new TreeNode(0);
26     dump -> left = root;
27     TreeNode* curNode = dump;
28     while (curNode) {
29         if (curNode -> left) {
30             TreeNode* predecessor = curNode -> left;
31             while (predecessor -> right && predecessor -> right != curNode)
32                 predecessor = predecessor -> right;
33             if (!(predecessor -> right)) {
34                 predecessor -> right = curNode;
35                 curNode = curNode -> left;
36             }
37             else {
38                 reverseAddNodes(curNode -> left, predecessor, nodes);
39                 predecessor -> right = NULL;
40                 curNode = curNode -> right;
41             }
42         }
43         else curNode = curNode -> right;
44     }
45     return nodes;
46 }

 

目录
相关文章
|
11月前
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
37 1
|
11月前
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
44 0
|
11月前
Leetcode Binary Tree Postorder Traversal(面试题推荐)
非递后续归遍历二叉树,肯定得用到栈。先序遍历很好写,但后续遍历就不是那么容易了。 只需要设置个指针pre,指向最后输出的那个节点就行了,只要判断cur指针指向的是上次输出节点的父节点,且cur无其他未遍历的节点,这个时候就把cur节点输出即可,然后更改pre。原理是要遍历当前节点,其所有子节点都必须遍历完,因为肯定是先左后右,所以只需一个指针保持前一次输出的结果即可。
47 0
|
11月前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
根据LCA的定义,二叉树中最小公共祖先就是两个节点p和q最近的共同祖先节点,LCA的定义没什么好解释的,主要是这道题的解法。
33 0
|
11月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
43 0
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
|
16天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
47 6
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
91 2