94:Binary Tree Inorder Traversal【树】【哈希】【栈】
来源:互联网 发布:免费的自己的域名邮箱 编辑:程序博客网 时间:2024/06/06 20:49
题目链接:click~
DFS解法:
/*题意:二叉树的中序遍历*/class Solution {public: void InOrderTraverse(TreeNode *root, vector<int> &v) { if(root == NULL) return; InOrderTraverse(root->left, v); v.push_back(root->val); InOrderTraverse(root->right, v); } vector<int> inorderTraversal(TreeNode *root) { vector<int> v; if(root == NULL) return v; InOrderTraverse(root, v); }};
栈模拟递归:
/** *思路:初始化时,将从根结点到最左结点路径上的所有结点压入栈中,栈顶即为 * 最左结点,打印栈顶元素,然后将其右子树下到最左结点路径压入栈。 */class Solution {private: vector<int> v;public: void pushleft(TreeNode *root, stack<TreeNode*> &s) { if(root != NULL) { s.push(root); pushleft(root->left, s); } } vector<int> inorderTraversal(TreeNode *root) { if(root == NULL) return v; stack<TreeNode*> s; pushleft(root, s); while(!s.empty()) { TreeNode *tmp = s.top(); s.pop(); v.push_back(tmp->val); pushleft(tmp->right, s); } return v; }};
0 0
- 94:Binary Tree Inorder Traversal【树】【哈希】【栈】
- 94Binary Tree Inorder Traversal
- [94]Binary Tree Inorder Traversal
- 94Binary Tree Inorder Traversal
- 94、Binary Tree Inorder Traversal
- 94 - Binary Tree Inorder Traversal
- 94-Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- xrzxrvbxrdd
- 3_MAVEN与eclipse集成
- java高分局之jvm指令浅析
- 关于安卓的activity task管理
- NIO与传统IO的区别
- 94:Binary Tree Inorder Traversal【树】【哈希】【栈】
- atitit.loading的设计与实现控件选型attilax 总结
- 献给这世上摇摆不定的人
- iOS 面试之三道题
- text下划线的实现方法
- python学习系列(一)--- main函数
- zlnfpxvljnv
- 海康RTSP格式
- Sass跟Scss 上手