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 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
随机推荐
- 基于Opencv自有模型识别人脸与人眼
#!/usr/bin/python # -*- coding: utf-8 -*- import cv2 face_cascade = cv2.CascadeClassifier("D:/O ...
- foreach与正常for循环效率对比
foreach foreach编译成字节码之后,使用的是迭代器实现的. foreach特点: 无须获取容器大小 需要创建额外的迭代器变量 遍历期间得到的是对象,没有索引位置信息,因此不能进行赋值操作. ...
- pycharm 中切换terminal的盘符
第一步,采用 cd .. 将当前路径设置为该盘符的根目录 第二步,采用 C: 将盘符设置为C盘然后使用 cd 命令将路径切换到指定位置
- 第三节 基本数据写入 --------增加&查询
启动mongodb服务 net start mongodb 链接mongodb 进入bin目录 mongo 127.0.0.1:12345 启动连接 show dbs 显示所有的数据库 use ...
- SPOJ1693 COCONUTS - Coconuts
传送门[洛谷] 自闭QAQ 什么玩意QAQ 不是很理解到底在干啥 问了巨佬以后大概是这个样子的 可以看出是最小割模型 对于每一个人 反悔的话就是代价+1 那么连接(s,i) (i,t)分别表示他最后选 ...
- MySQL添加主键、索引
查看索引 SHOW INDEX FROM 数据库表名 比如:SHOW INDEX FROM order_info; 添加索引 alter table 数据库add index 索引名称(数据库字段 ...
- 【leetcode】1021. Best Sightseeing Pair
题目如下: Given an array A of positive integers, A[i]represents the value of the i-th sightseeing spot, ...
- css linear-gradient;心跳animation
css线性背景 background:linear-gradient(20deg,#ccffff,#ffcccc); transform transform:scale(1.5); transform ...
- Centos7.4 修改selinux错误导致服务器起不来
[root@node10 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # ...
- linux学习-文件管理
1.文件系统结构 /boot:引导文件存放目录,内核文件(vmlinuz).引导加载器(bootloader, grub)都存放于此目录 /bin:所有用户使用的基本命令:不能关联至独立分区,OS启动 ...