[Algorithm] 94. Binary Tree Inorder Traversal iteratively approach
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的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【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 ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- 【LeetCode】94. Binary Tree Inorder Traversal
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
随机推荐
- Python-06-函数
一.函数的定义 函数是第一类对象,即函数可以当作数据传递 #1 可以被引用 #2 可以当作参数传递 #3 返回值可以是函数 #3 可以当作容器类型的元素 1. 定义方式 def 函数名(参数1,参数2 ...
- 通过Queue控制线程并发,并监控队列执行进度
# -*- coding:utf-8 -*- import Queue import time import threading # 需要执行的业务主体 def domain(id): time.sl ...
- MySQL数据类型之BLOB与TEXT及其最大存储限制
https://blog.csdn.net/q3dxdx/article/details/51014357 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.n ...
- Entity Framework Core今日所得:避免 IEnumerable 以及 IQueryable 陷阱
避免 IEnumerable 以及 IQueryable 陷阱: IEnumerable示用Linq会先去数据库查询所有记录,然后再条件查询. IQueryable接口派生自IEnumerable,但 ...
- C# vb .net图像合成-合成椭圆
在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...
- Windows 搭建 nginx rtmp服务器
1.环境开发环境:windows开发工具:ffmpeg.nginx.nginx-rmtp-module.vlc media player播放器 2.准备文件官方ffmpeg下载地址:http://ww ...
- 基于elementUI创建的vue项目
这周对公司的内容使用vue进行重构,所以记录一下开始项目的过程 下载elementUI: 项目文件夹中在命令行中输入:npm install elementui -s 下载完成后在 main.js 中 ...
- 从 Vue 的视角学 React(一)—— 项目搭建
虽然 Vue 在国内一家独大,但在全球范围内,React 依然是最流行的前端框架 最近和一些朋友聊天,发现很多项目都选择了 React 技术栈,而且公司的新项目也决定使用 React 我一直以来都是走 ...
- JavaScript 简单类型和复杂类型区别
一.基本类型 1.概述 值类型又叫做基本数据类型,简单数据类型.在存储时,变量中存储的是值本身,因此叫做值类型 2.基本类型在内存中的存储 基本数据类型存储在栈区中. 3.基本类型作为函数的参数 基本 ...
- consul:connect
官方文档:https://www.consul.io/docs/connect/index.html#getting-started-with-connect consul connect的功能类似与 ...