LC 94. Binary Tree Inorder Traversal
问题描述
Given a binary tree, return the inorder traversal of its nodes' values. (左 - 根 - 右)
Example:
- Input: [1,null,2,3]
- 1
- \
- 2
- /
- 3
- Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
参考答案
- /**
- * 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) {
- // init
- vector<int> res;
- stack<TreeNode* > st;
- TreeNode* p = root; // 初始化根节点
- while(p||!st.empty()){
- // 一旦遇到节点,先考虑左边的,直到尽头,如果没有之后的右node,就停止运行了
- while(p){
- st.push(p);
- p = p->left;
- }
- // 立刻提取p的信息,并且把p弹出来。如果进入while,那么这一步只会是左孩子,如果没进入while,那么会是父节点/右节点。
- p = st.top();
- st.pop();
- res.push_back(p->val);
- // 把p 变成p的右孩子
- p = p->right;
- }
- return res;
- }
- };
LC 94. Binary Tree Inorder Traversal的更多相关文章
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- Leetcode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode 94 Binary Tree Inorder Traversal ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Java [Leetcode 94]Binary Tree Inorder Traversal
题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...
随机推荐
- Magic Points ZOJ - 4032
The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple Magic Points ZOJ - ...
- 数据结构实验之数组三:快速转置(SDUT 3347)
Problem Description 转置运算是一种最简单的矩阵运算,对于一个m*n的矩阵M( 1 = < m < = 10000,1 = < n < = 10000 ),它 ...
- windows游戏编程鼠标
①.常用鼠标消息: WM_MOUSEMOVE 鼠标移动位置 WM_LBUTTONDOWN 鼠标左键按下 WM_LBUTTONUP 鼠标左键弹起 ...
- laotech老师唠科mac 深入浅出MAC OS X
laotech老师唠科mac 深入浅出MAC OS X http://study.163.com/plan/planLearn.htm?id=1637004#/learn/resVideo?lesso ...
- Smali基础知识
Smali是用于Dalvik(Android虚拟机)的反汇编程序实现 汇编工具(将Smali代码汇编为dex文件)为smali.jar baksmali.jar则是反汇编程序 地址:https://b ...
- oracle查询历史执行语句
SELECT * FROM v$sqlarea WHERE PARSING_SCHEMA_NAME='GAVIN' and SQL_TEXT LIKE '%delete%' ORDER BY LAST ...
- Qt for Android (二) Qt打开android的相册
以下有一个可以用的Demo https://files.cnblogs.com/files/wzxNote/Qt-Android-Gallery-master.zip 网盘地址: https://pa ...
- 算法的时间复杂度——"大O分析法"(转载)
原文地址:https://my.oschina.net/gooke/blog/684026 一下为本人笔记:) 场景:在解决计算机科学领域的问题时,经常有好多个方法都可以,想找到最优的方法,就有了时间 ...
- LightGBM两种使用方式
原生形式使用lightgbm(import lightgbm as lgb) import lightgbm as lgb from sklearn.metrics import mean_squar ...
- Kombu is a messaging library for Python.
https://kombu.readthedocs.io/en/stable/introduction.html