LeetCodeAnimation/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main4.cpp
2018-12-07 10:41:02 +08:00

56 lines
1.1 KiB
C++
Executable File

/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
/// Author : liuyubobobo
/// Time : 2017-05-30
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// Another Classic Non-Recursive algorithm for inorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if( root == NULL )
return res;
stack<TreeNode*> stack;
TreeNode* cur = root;
while(cur != NULL || !stack.empty()){
if(cur != NULL){
stack.push(cur);
cur = cur->left;
}
else {
cur = stack.top();
stack.pop();
res.push_back(cur->val);
cur = cur->right;
}
}
return res;
}
};
int main() {
return 0;
}