Binary Tree Inorder Traversal
来源:互联网 发布:淘宝直通车溢价比例 编辑:程序博客网 时间:2024/06/16 17:13
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?
Subscribe to see which companies asked this question
/** * 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 {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> result; const TreeNode *p = root; stack<const TreeNode *> s; while (!s.empty() || p != nullptr) { if (p != nullptr) { s.push(p); p = p->left; } else { p = s.top(); s.pop(); result.push_back(p->val); p = p->right; } } return result; }};
0 0
- 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
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Android常用的热更新技术框架调研
- IOS OC声明变量在@interface括号中与使用@property的区别
- python大杀器之requests安装
- 尺寸
- 安卓 文件读写
- Binary Tree Inorder Traversal
- Linux内核编译->ecryptfs模块编译
- 获取当前屏幕显示的viewcontroller
- HDU 5745 La Vie en rose 字符串匹配(暴力)
- 安装完 MySQL 后必须调整的 10 项配置
- Git学习笔记(二)
- 删除节点
- 微信浏览器取消缓存的方法
- STM32启动文件