Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1\2/3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

/*** Definition for binary tree* 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> res;if(root==NULL)return res;TreeNode *p=root;stack<TreeNode *> sta;while(p!=NULL||!sta.empty()){while(p!=NULL){sta.push(p);p=p->left;}if(!sta.empty()){p=sta.top();sta.pop();res.push_back(p->val);p=p->right;}}return res;
}
/*
void inorder(TreeNode *root, vector<int> &res)
{if(root==NULL)return;inorder(root->left,res);res.push_back(root->val);inorder(root->right,res);return;
}vector<int> inorderTraversal(TreeNode *root) {vector<int> res;inorder(root,res);return res;}
*/
};

转载于:https://www.cnblogs.com/Vae1990Silence/p/4281382.html

leetcode[94]Binary Tree Inorder Traversal相关推荐

  1. 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal

    LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...

  2. LeetCode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...

  3. [LeetCode]:94:Binary Tree Inorder Traversal

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...

  4. [swift] LeetCode 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  5. LeetCode 94. Binary Tree Inorder Traversal--二叉树中序遍历--递归,迭代--C++,Python解法

    题目地址:Binary Tree Inorder Traversal - LeetCode Given a binary tree, return the inorder traversal of i ...

  6. 94. Binary Tree Inorder Traversal

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...

  7. 94. Binary Tree Inorder Traversal二叉树的中序遍历

    网址:https://leetcode.com/problems/binary-tree-inorder-traversal/ 参考:https://leetcode.com/problems/bin ...

  8. 94 Binary Tree Inorder Traversal

    递归的代码是以前数据结构书上常见的: public ArrayList<Integer> inorderTraversal(ConstructBinaryTreefromPostorder ...

  9. 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3]1\2/3输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 二叉树遍历 不太想用递归,试试循环吧 ...

最新文章

  1. 现实迷途 第七章 特殊客户
  2. setTimeout和setInterval的使用
  3. python3.7安装turtle步骤-Python怎么引入turtle
  4. 蓝桥杯java第八届第五题--取数位
  5. axios post body参数_09-axios
  6. 1715: 序列变换(LIS变形)
  7. vscode+XDebug调试远程环境(虚拟机)上的PHP代码
  8. 数据结构课程设计---------用栈来实现表达式求值
  9. Maven补全之生命周期(Lifecycle)
  10. maven中的groupId和artifactId到底指的是什么?
  11. pandas 转换为文本类型_分享5个高效的pandas函数!
  12. C#和C++中的float类型
  13. sap的pod确认_VLPOD确认交付凭证
  14. 计算机主机配置科普,电脑装机配置 篇八:电脑内存如何选购?内存科普--小白篇...
  15. Java实现 LeetCode 21 合并两个有序链表
  16. 在后台处理请求返回的json字符串
  17. PythonStock(29)股票系统:涛动周期论 经济周期决定人生财富命运,下一步的研究方向,从宏观角度进行市场行情研究。
  18. Modeling Personalized Item Frequency Information for Next-basket Recommendation SIGIR2020
  19. Objective-C(八、正式协议Protocol,非正式协议informal protocol)——iOS开发基础
  20. UA OPTI570 量子力学16 含时的哈密顿量与时间演化算符

热门文章

  1. java同类型同字段名称对象赋值
  2. Android开发笔记(五十二)通知推送Notification
  3. Java单例模式之最优解分析【为何说是最优解】
  4. Javascript设计模式之——代理模式
  5. jar命令指定入口类
  6. Scheduler:Event UID not valid(转)
  7. NBU7.5---Windows_BMR备份与恢复
  8. 申请google Map api key for android
  9. 18kw丹佛斯变频器常见故障_变频器常见故障——输出不平衡、过载、开关电源损坏...
  10. 小孔成像总结_【伟林教育】初中生必看:中考物理解题技巧+方法总结,可以说很到位了!...