39.Binary Tree Inorder Traversal(二叉树中序遍历)
Level:
Medium
题目描述:
Given a binary tree, return the inorder traversal of its nodes' values.
思路分析:
实现一棵二叉树的中序遍历,我们可以用简单的递归方法去实现,也可以使用栈去实现,使用第二种方式时,我们沿着根节点先遍历左子树的左孩子,将它们依次压入栈,知道左孩子为空,弹出栈顶节点,这时记录栈顶节点的值,如果栈顶节点的右孩子不为空,压入栈,如果为空,则栈顶元素继续弹出,重复上述操作,就能获得中序遍历的结果。
代码:
/**public class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x){
val=x;
}
}*/
public class Solution{
public List<Integer> inorderTraversal(TreeNode root){
List<Integer>res=new ArrayList<>();
Stack<TreeNode>s=new Stack<>();
if(root==null)
return res;
TreeNode pNode=root;
while(pNode!=null||!s.isEmpty()){
if(pNode!=null){
s.push(pNode);
pNode=pNode.left;
}else{
TreeNode Node=s.pop();
res.add(Node.val);
pNode=Node.right;
}
}
return res;
}
}
39.Binary Tree Inorder Traversal(二叉树中序遍历)的更多相关文章
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- [Leetcode] 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 | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
随机推荐
- python关键字global和nonlocal总结
函数中使用全局变量 a = 100 b = 200 def func(): def sub(): return b return a + b + sub() 执行fun()后返回值为:500 a, b ...
- HDU4035 Maze 期望DP+树形DP(好题)
题意:有一个树形的迷宫,有N个房间(标号为1~N)以及N-1条通道将它们连通,一开始在1号房间,每进入一个房间i,有k[i]的概率被陷阱杀死回到房间1,有s[i]的概率找到出口逃离迷宫,如果没有找到出 ...
- HDU 6613 Squrirrel 树形dp
题意:给你一颗树,你可以把这棵树上的一条边的边权变为0,现在让你选一个根,让所有点到这个点的最大距离尽量的小.如果有多个根的最大距离距离相同,输出编号最小的边. 思路:如果没有把边权变为0的操作,这个 ...
- 简单说下cookie,LocalStorage与SessionStorage.md
最近在网上看到有人讨论这三个的一些概念与区别,发现自己也一直没有较好的总结,所以查阅了一些资料来阐述一下. 基本概念 cookie cookie英文意思是小甜饼,是原来的网景公司创造,目前是在客户端存 ...
- k8s阅读笔记2-k8s架构
前言 阅读地址 https://rootsongjc.gitbooks.io/kubernetes-handbook/content/concepts/ 架构 架构图说明: master 指服务端 1 ...
- 用Matlab的.m脚本文件处理实验室数据
找到相应的文件 findfile %1 打开文件夹 %2 拷贝第一个文件 %3 关闭当前文件,再次拷贝新的文件,直到文件末尾结束 clc clear DST_PATH_t = 'C:\Users\Ma ...
- ltp-ddt realtime_cpu_load timeout
# @name Worstcase Latency with cpu load by using cyclictest# @desc Measure latency under cpu load by ...
- 前端每日实战:49# 视频演示如何用纯 CSS 创作一支诱人的冰棍
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/vrxzMw 可交互视频教程 此视频 ...
- 数组之间的比较应当用Arrays.equals()
被坑了,数组之间的比较不能用“==”,应当用Arrays.equals() 如果是原生数组(即数组中的值是几大基本数据类型之一的)之间的比较可以直接用,如果数组中的值不是原生的基本数据类型,那么再使用 ...
- 在Eclipse中配置安卓的开发环境 (踩过的坑)
这个学期学校有门安卓程序设计课需要安装安卓开发环境. 一开始安装的是Andriod Studio,但是过程很坎坷很心酸,遇到各种各样的问题,最后还没有解决. 没办法决定用Eclipse配置安卓环境,虽 ...