-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path094_binaryTreeInorderTraversal.cpp
More file actions
42 lines (36 loc) · 1.13 KB
/
Copy path094_binaryTreeInorderTraversal.cpp
File metadata and controls
42 lines (36 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Sourse : https://leetcode.com/problems/binary-tree-inorder-traversal/
// Difficulty : Medium
/***********************************************************************
*
* Given a binary tree, return the inorder traversal of its nodes' values.
*
**********************************************************************/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
// Recursive solution都是渣渣wwwww
vector<int> inorderTraversal(TreeNode* root) {
TreeNode* cur = root;
stack<TreeNode*> stk;
vector<int> res;
while (cur || !stk.empty()) {
while (cur) {
stk.push(cur);
cur = cur->left; // the deepest left node
}
cur = stk.top();
stk.pop();
res.push_back(cur->val); // add last node
cur = cur->right; // get right. if right=null, next step will pop the last node
}
return res;
}
};