[leetcode-94-Binary Tree Inorder Traversal]

Posted hellowOOOrld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode-94-Binary Tree Inorder Traversal]相关的知识,希望对你有一定的参考价值。

Given a binary tree, return the inorder traversal of its nodes‘ values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?

vector<int> inorderTraversal(TreeNode* root)
{
    vector<int>result;
    stack<TreeNode*>st;
    while(root != NULL || !st.empty())
    {
        if(root!= NULL)
        {
            st.push(root);
            root = root->left;
        }
        else
        {
            root = st.top();
            result.push_back(root->val);
            st.pop();
            root = root->right;
        }
    }
    return result;
}

 










以上是关于[leetcode-94-Binary Tree Inorder Traversal]的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode-94-Binary Tree Inorder Traversal]

[LeetCode]94.Binary Tree Inorder Traversal

leetcode 94. Binary Tree Inorder Traversal

LeetCode94 Binary Tree Inorder Traversal

LeetCode 94. Binary Tree Inorder Traversal

LeetCode 94: Binary Tree Inorder Traversal