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.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/ function getLefts(root) {
if (!root) {
return [];
} let result = [];
let current = root;
while(current) {
result.push(current);
current = current.left ;
} return result;
} /**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
let lefts = getLefts(root);
let result = [];
while (lefts.length) {
let newRoot = lefts.pop();
result.push(newRoot.val);
if (newRoot.right) {
let newLefts = getLefts(newRoot.right);
lefts = lefts.concat(newLefts)
}
} return result;
};

The idea is

  • get all the left side node and push into stack
  • because stack is first in last out, when we do pop(), we are actually start from bottom of the tree.
  • everytime, we pop one node from stack, we check its right child, get all the left nodes from this right child and append into stack.
  • in this way, we are able to track all the node.

[Algorithm] 94. Binary Tree Inorder Traversal iteratively approach的更多相关文章

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

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

  2. 刷题94. Binary Tree Inorder Traversal

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

  3. 49. leetcode 94. Binary Tree Inorder Traversal

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

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

  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 ----- java

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

  7. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

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

  8. 【LeetCode】94. Binary Tree Inorder Traversal

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

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

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

随机推荐

  1. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  2. Logstash配置文件修改自动加载和指定目录进行启动

    检查配置并启动Logstash,修改后自动加载 指定配置文件目录并启动Logstash

  3. 高性能MYSQL(查询优化)

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  4. Winows上简单配置使用kafka(.net使用)

    一.kafka环境配置 1.jdk安装 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载JDK安装完 ...

  5. quota磁盘配额

    一.什么是磁盘配额 磁盘配额从字面意思上看就是给一个磁盘配置多少额度,而quota就是有多少限额的意思,所以总的来说就是限制用户对磁盘空间的使用量.因为Linux是多用户多任务的操作系统,许多人公用磁 ...

  6. 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)

    题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...

  7. springMVC执行流程及架构

    目录 springMVC简单执行流程 springMVC框架 注解实现 springMVC简单执行流程 springMVC框架 执行流程: 1.用户发送请求至前端控制器DispatcherServle ...

  8. Java 数组实例——将阿拉伯数字转换为最大写

    题目:将阿拉伯数字转换为最大写,比如1234转换为壹仟贰佰叁拾肆. package my_package; public class Transform { private String[] arr1 ...

  9. 关于SQL中的 where 1 = 1 的用法

    在项目中的常见的一个操作:在有关SQL的代码中加入where 1 = 1,关于它的用法,可以总结如下: 首先,where 1 = 1的用法往往是为了方便后续的给SQL增加where限制条件.如果实现加 ...

  10. Golang: 抓取网页内容

    今天写个简单的程序,根据指定的 URL 来抓取相应的网页内容,然后存入本地文件.这个程序会涉及到网络请求和文件操作等知识点,下面是实现代码: // fetch.go package main impo ...