solutions
  • solutions
  • Valid Palindrome
  • String to Integer (atoi)
  • addBinary
  • Longest Palindromic Substring
  • Regular Expression Matching
  • Valid Number
  • Count and say
  • Valid anagram
  • Simplify Path
  • Length of last word
  • Longest Valid Parentheses
  • Valid Parentheses
  • Largest Rectangle in Histogram
  • Evaluate Reverse Polish Notation
    • Morris 算法介绍
  • Binary Tree Preorder Traversal
    • //stack
    • //morris
  • Binary Tree Inorder Traversal
    • //stack
    • //morris
  • Binary Tree Postorder Traversal
    • //stack
  • Binary Tree Level Order Traversal
    • 递归
    • 迭代
  • Binary Tree Zigzag Level Order Traversal
    • 递归
    • 迭代
  • Recover Binary Search Tree
    • Recursive
    • morris
  • Same Tree
  • Symmetric Tree
    • Recursive
    • Iterative
  • Balanced Binary Tree
  • Flatten Binary Tree to Linked List
    • Iterative and recursive
  • Populating Next Right Pointers in Each Node II
    • Iterative
    • Recursive
  • Construct Binary Tree from Preorder and Inorder Traversal
  • Construct Binary Tree from Inorder and Postorder Traversal
  • Unique Binary Search Trees
  • Validate Binary Search Tree
  • Convert Sorted Array to Binary Search Tree
  • Convert Sorted List to Binary Search Tree
  • Minimum Depth of Binary Tree
  • Maximum Depth of Binary Tree
  • Path Sum
  • Path Sum II
  • Binary Tree Maximum Path Sum
  • Populating Next Right Pointers in Each Node
  • Sum Root to Leaf Numbers
  • Merge Sorted Array
  • Merge Two Sorted Lists
    • cpp
    • java
  • Merge k Sorted Lists
    • Priority queue
    • merge sort
  • Insertion Sort List
  • Sort List
  • First Missing Positive
  • Sort Colors
  • Search for a Range
  • Search Insert Position
    • Recursive
    • Iterative
  • Search a 2D Matrix
  • *string 的子集(bit)
  • Subsets
    • Bit
    • Recursive
    • Iterative
  • Subsets II
    • Recursive
    • Iterative
  • Permutations
  • Permutations II
    • Java
  • CPP
  • Combinations
    • Recursive
  • Iterative
  • Letter Combinations of a Phone Number
    • Recursive
  • BFS VS DFS
  • Word Ladder
    • Java
Powered by GitBook
On this page

Was this helpful?

  1. Binary Tree Postorder Traversal

//stack

vector<int> postorderTraversal(TreeNode* root) {

stack<TreeNode*> s;

vector<int> ret;

TreeNode q=nullptr, p=root;

while(p || !s.empty()){

if(p){

s.push(p);

p=p->left;

}else{

p=s.top();

if(p->right&&p->right!=q){ //右子未访问 所以不但当前P不弹出 而且将P右子塞入(左子已经访问完?)然后从右子的左子树继续(设为新P)

p=p->right;

s.push(p);

p=p->left;

}else{

ret.push_back(p->val);

q=p;

s.pop();

p=nullptr;

}

}

}

return ret;

}

//morris 没过!!!!!

class Solution {

public:

vector<int> postorderTraversal(TreeNode* root) {

TreeNode dummy(-1);

TreeNode p, temp;

vector<int> result;

stringstream out;

p=&dummy;

p->left=root;

while(p){

temp=p->left;

if(!temp){

p=p->right;

}else{

while(temp->right && temp->right!=p)

temp=temp->right;

if(!temp){

temp->right=p;

p=p->left;

}else{

reversePrint(p->left,temp,result);

if(result.size()>0 )

break;

temp->right=nullptr;

p=p->right;

}

}

}

return result;

}

void reverse(TreeNode from, TreeNode to){

if(from==to) return;

TreeNode x=from, y=x->right, *z;

while(x!=to){

z=y->right;

y->right=x;

x=y;

y=z;

}

}

void reversePrint(TreeNode from, TreeNode to, vector<int> &ret ){

reverse(from,to);

TreeNode *p=to;

while(true){

ret.push_back(p->val);

if(p==from)

break;

p=p->right;

}

reverse(to,from);

}

};

PreviousBinary Tree Postorder TraversalNextBinary Tree Level Order Traversal

Last updated 5 years ago

Was this helpful?