问题描述

Given a binary tree, return the inorder traversal of its nodes' values. (左 - 根 - 右)

Example:

  1. Input: [1,null,2,3]
  2. 1
  3. \
  4. 2
  5. /
  6. 3
  7.  
  8. Output: [1,3,2]

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

参考答案

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<int> inorderTraversal(TreeNode* root) {
  13. // init
  14. vector<int> res;
  15. stack<TreeNode* > st;
  16. TreeNode* p = root; // 初始化根节点
  17.  
  18. while(p||!st.empty()){
  19. // 一旦遇到节点,先考虑左边的,直到尽头,如果没有之后的右node,就停止运行了
  20. while(p){
  21. st.push(p);
  22. p = p->left;
  23. }
  24. // 立刻提取p的信息,并且把p弹出来。如果进入while,那么这一步只会是左孩子,如果没进入while,那么会是父节点/右节点。
  25. p = st.top();
  26. st.pop();
  27. res.push_back(p->val);
  28. // 把p 变成p的右孩子
  29. p = p->right;
  30. }
  31. return res;
  32. }
  33. };

LC 94. Binary Tree Inorder Traversal的更多相关文章

  1. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  2. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  3. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  4. 刷题94. Binary Tree Inorder Traversal

    一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...

  5. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

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

  6. Leetcode 94. Binary Tree Inorder Traversal

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

  7. 94. Binary Tree Inorder Traversal

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

  8. leetcode 94 Binary Tree Inorder Traversal ----- java

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

  9. Java [Leetcode 94]Binary Tree Inorder Traversal

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

随机推荐

  1. Magic Points ZOJ - 4032

    The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple Magic Points ZOJ - ...

  2. 数据结构实验之数组三:快速转置(SDUT 3347)

    Problem Description 转置运算是一种最简单的矩阵运算,对于一个m*n的矩阵M( 1 = < m < = 10000,1 = < n < = 10000 ),它 ...

  3. windows游戏编程鼠标

    ①.常用鼠标消息: WM_MOUSEMOVE           鼠标移动位置 WM_LBUTTONDOWN       鼠标左键按下 WM_LBUTTONUP             鼠标左键弹起 ...

  4. laotech老师唠科mac 深入浅出MAC OS X

    laotech老师唠科mac 深入浅出MAC OS X http://study.163.com/plan/planLearn.htm?id=1637004#/learn/resVideo?lesso ...

  5. Smali基础知识

    Smali是用于Dalvik(Android虚拟机)的反汇编程序实现 汇编工具(将Smali代码汇编为dex文件)为smali.jar baksmali.jar则是反汇编程序 地址:https://b ...

  6. oracle查询历史执行语句

    SELECT * FROM v$sqlarea WHERE PARSING_SCHEMA_NAME='GAVIN' and SQL_TEXT LIKE '%delete%' ORDER BY LAST ...

  7. Qt for Android (二) Qt打开android的相册

    以下有一个可以用的Demo https://files.cnblogs.com/files/wzxNote/Qt-Android-Gallery-master.zip 网盘地址: https://pa ...

  8. 算法的时间复杂度——"大O分析法"(转载)

    原文地址:https://my.oschina.net/gooke/blog/684026 一下为本人笔记:) 场景:在解决计算机科学领域的问题时,经常有好多个方法都可以,想找到最优的方法,就有了时间 ...

  9. LightGBM两种使用方式

    原生形式使用lightgbm(import lightgbm as lgb) import lightgbm as lgb from sklearn.metrics import mean_squar ...

  10. Kombu is a messaging library for Python.

    https://kombu.readthedocs.io/en/stable/introduction.html