标签:show top opened closed null tree node 开始 init ide
二叉树中序非递归
/** * 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 { vector<int>ans; public: vector<int> inorderTraversal(TreeNode* root) { stack<TreeNode*>tree; if(root==NULL) return ans; tree.push(root); while(!tree.empty()) { TreeNode*stacktop=tree.top(); while(stacktop->left!=NULL) { tree.push(stacktop->left); stacktop=tree.top(); } while(!tree.empty() && tree.top()->right==NULL) { stacktop=tree.top(); tree.pop(); ans.push_back(stacktop->val); } if(tree.empty()) return ans; stacktop=tree.top(); ans.push_back(stacktop->val); tree.pop(); tree.push(stacktop->right); } return ans; } };
标签:show top opened closed null tree node 开始 init ide
原文地址:https://www.cnblogs.com/tingxilin/p/12603960.html