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 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
随机推荐
- JS高级 — 函数的声明和表达式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- JS中的Boolean数据类型
Boolean布尔数据类型 只有两个字面值:true和false,这两个值与数字值不是一回事,因此true不一定等于1,而false也不一定等于0. 把其他类型转换为布尔类型 只有0.NaN.''.n ...
- Linux学习笔记1-在CentOS 7中安装配置JDK8
说明: 参考博客:http://blog.csdn.net/czmchen/article/details/41047187系统环境:CentOS 7安装方式:rpm安装JDK地址:http://ww ...
- 2018-2-13-win10-uwp-分治法
title author date CreateTime categories win10 uwp 分治法 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:2 ...
- 基于TI 多核DSP 的大数据智能计算处理解决方案
北京太速科技有限公司 大数据智能计算,是未来的一个发展趋势,大数据计算系统主要完成数据的存储和管理:数据的检索与智能计算. 特别是在智能城市领域,由于人口聚集给城市带来了交通.医疗.建筑等各方面的压力 ...
- Sass--混合宏--声明宏
如果你的整个网站中有几处小样式类似,比如颜色,字体等,在 Sass 可以使用变量来统一处理,那么这种选择还是不错的.但当你的样式变得越来越复杂,需要重复使用大段的样式时,使用变量就无法达到我们目了.这 ...
- WPF 几种常用控件样式的总结
这里把wpf中几种常用样式总结一下,后期可以直接拷贝使用,呵呵 一.Button <ResourceDictionary xmlns="http://schemas.microsoft ...
- git,提交错了分支,想把远程的分支恢复到上一个版本
1.先将本地分支回滚到上一个版本 2.删除远程分支(可以先备份一下) 3.创建新的分支,将本地分支push上去
- 51nod 1384:全排列(STL)
题目链接 记住next_permutation函数的用法,另外string在这里比char[]慢好多啊.. //#include<bits/stdc++.h> //using namesp ...
- 【8.0.0_r4】AMS分析(十七)(ActivityManagerService.java下)
代码位于frameworks/base/services/core/java/com/android/server/am/,一共有七十个文件. Java源码位于package com.android. ...