标签:bin treenode empty example null span top via 相对
Given a binary tree, return the inorder traversal of its nodes‘ values.
Example:
Input: [1,null,2,3] 1 2 / 3 Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; if (!root) return res; stack<TreeNode*> st; while (!st.empty() || root){ while (root){ st.push(root); root = root->left; } root = st.top(); st.pop(); res.push_back(root->val); root = root->right; } return res; } };
leetcode94 - Binary Tree Inorder Traversal - medium
标签:bin treenode empty example null span top via 相对
原文地址:https://www.cnblogs.com/xuningwang/p/13508518.html