今天写经典的tree traversal - inorder
inorder: 左边先拜访,接着中间,最後右边。
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
if(root == NULL)
return {};
stack<TreeNode*> s;
TreeNode* curr_node = root;
while( curr_node || !s.empty() ) {
if (curr_node) {
s.push(curr_node);
curr_node = curr_node->left;
}
else {
TreeNode *node = s.top();
s.pop();
ans.push_back(node->val);
curr_node = node->right;
}
}
return ans;
}
};
参考:
https://ithelp.ithome.com.tw/articles/10213280
https://shubo.io/iterative-binary-tree-traversal/
<<: Day19 用python写UI-聊聊OptionMenu
首先先在StockPrice以外,新增Bid和Ask,还有Lock(多线程读写资料避免打架的工具) ...
如果是 debian 了话,要在 /etc/apt/source.list 加入 deb http:...
相信有人已经迫不及待要撰写文章了,不过在这之前,我们先来介绍一下 Markdown 这个标记语言。 ...
终於要进入成立订单的部份了,先看一下成立订单所需要的参数, { "ShopNo"...
昨天的巢状判断式有没有成功扰乱你呢?不管懂不懂,都要记得常常去复习哦,这样才会有深刻的印象!(๑•̀...