mirror of
https://gitee.com/TheAlgorithms/LeetCodeAnimation.git
synced 2024-12-31 15:25:33 +08:00
65 lines
1.5 KiB
C++
Executable File
65 lines
1.5 KiB
C++
Executable File
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
|
|
/// Author : liuyubobobo
|
|
/// Time : 2017-11-17
|
|
|
|
#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) {}
|
|
};
|
|
|
|
|
|
// My Non-Recursive
|
|
// 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 {
|
|
|
|
private:
|
|
struct Command{
|
|
string s; // go, print
|
|
TreeNode* node;
|
|
Command(string s, TreeNode* node): s(s), node(node){}
|
|
};
|
|
|
|
public:
|
|
vector<int> inorderTraversal(TreeNode* root) {
|
|
|
|
vector<int> res;
|
|
if( root == NULL )
|
|
return res;
|
|
|
|
stack<Command> stack;
|
|
stack.push(Command("go", root));
|
|
while( !stack.empty() ){
|
|
Command command = stack.top();
|
|
stack.pop();
|
|
|
|
if(command.s == "print")
|
|
res.push_back(command.node->val);
|
|
else{
|
|
assert(command.s == "go");
|
|
if(command.node->right)
|
|
stack.push(Command("go",command.node->right));
|
|
stack.push(Command("print", command.node));
|
|
if(command.node->left)
|
|
stack.push(Command("go",command.node->left));
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
};
|
|
|
|
int main() {
|
|
|
|
return 0;
|
|
}
|