标签:
Given a binary tree, return the inorder traversal of its nodes‘ values.
For example:
Given binary tree {1,#,2,3}
,
1
2
/
3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
class Solution { public: vector<int> inorderTraversal(TreeNode *root) { vector<int> path; stack<TreeNode *> st; if (root == NULL) return path; TreeNode *p = root; while (p != NULL || !st.empty()) { while (p != NULL) { st.push(p); p = p->left; } if (!st.empty()) { p = st.top(); st.pop(); path.push_back(p->val); p = p->right; } } return path; } };
class Solution { public: vector<int> result; void inorder(TreeNode *root) { if (root == NULL) return; inorder(root->left); result.push_back(root->val); inorder(root->right); } vector<int> inorderTraversal(TreeNode *root) { result.clear(); inorder(root); return result; } };
http://www.waitingfy.com/archives/1622
LeetCode Binary Tree Inorder Traversal
标签:
原文地址:http://blog.csdn.net/fox64194167/article/details/44151433