mirror of
https://gitee.com/TheAlgorithms/LeetCodeAnimation.git
synced 2024-12-31 15:25:33 +08:00
56 lines
1.1 KiB
C++
Executable File
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) {}
|
|
};
|
|
|
|
|
|
// 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()){
|
|
|
|
while(cur != NULL){
|
|
stack.push(cur);
|
|
cur = cur->left;
|
|
}
|
|
|
|
cur = stack.top();
|
|
stack.pop();
|
|
res.push_back(cur->val);
|
|
cur = cur->right;
|
|
|
|
}
|
|
return res;
|
|
}
|
|
};
|
|
|
|
int main() {
|
|
|
|
return 0;
|
|
}
|