博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
94. Binary Tree Inorder Traversal
阅读量:7199 次
发布时间:2019-06-29

本文共 1136 字,大约阅读时间需要 3 分钟。

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

Example:

Input: [1,null,2,3]   1    \     2    /   3Output: [1,3,2]

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

难度:medium

题目:给定二叉树,返回其中序遍历结点。(不要使用递归)

思路:栈

Runtime: 1 ms, faster than 55.50% of Java online submissions for Binary Tree Inorder Traversal.

Memory Usage: 36.2 MB, less than 100.00% of Java online submissions for Binary Tree Inorder Traversal.

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */class Solution {    public List
inorderTraversal(TreeNode root) { Stack
stack = new Stack<>(); List
result = new ArrayList<>(); while (!stack.isEmpty() || root != null) { if (root != null) { stack.push(root); root = root.left; } else { TreeNode node = stack.pop(); result.add(node.val); root = node.right; } } return result; }}

转载地址:http://amzum.baihongyu.com/

你可能感兴趣的文章
react-redux源码分析及实现原型(上)
查看>>
Spring Boot 学习笔记(2):JDBC
查看>>
我的第一篇markdown文档
查看>>
callback
查看>>
利用Guideline实现ConstraintLayout中控件居中
查看>>
【百度地图API】如何使用suggestion--下拉列表方式的搜索建议
查看>>
均匀随机排列数组
查看>>
do_fork()函数流程分析
查看>>
阿里云ECS Centos 7.x Docker安装
查看>>
红黑树
查看>>
比较全面的gdb调试命令
查看>>
acm 网站
查看>>
Linux centos yum安装LAMP环境
查看>>
爬虫到百度贴吧,爬取自己的小说
查看>>
Windows批处理BAT脚本查询PM2.5实时空气质量指数(AQI)
查看>>
excel 函数 vlookup
查看>>
基于keepalived实现LVS高可用以及Web服务高可用
查看>>
1-2-Active Directory 域服务准备概述
查看>>
怎样测试UDP端口
查看>>
Electron实现系统鼠标指针操作
查看>>