leetcode 94. Binary Tree Inorder Traversal

来源:互联网 发布:云计算股票龙头股 编辑:程序博客网 时间:2024/05/18 01:53

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>re;if (root == NULL)return re;vector<TreeNode*>que;que.push_back(root);bool f = true;while (!que.empty()){if (f&&que.back()->left != NULL)que.push_back(que.back()->left);else{re.push_back(que.back()->val);TreeNode*n = que.back(); que.pop_back();if (n->right != NULL){que.push_back(n->right);f = true;}elsef = false;}}return re;}};

accepted


0 0
原创粉丝点击