给定一个二叉树的根节点root,返回它的中序遍历结果。
数据范围:树上节点数满足
,树上每个节点的值满足 
进阶:空间复杂度
,时间复杂度 )
进阶:空间复杂度
{1,2,#,#,3}[2,3,1]
{}[]
{1,2}[2,1]
{1,#,2}[1,2]
树中节点数目在范围 [0, 100] 内树中的节点的值在[-100,100]以内
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
List<Integer> list = new ArrayList();
travelsal(root,list);
int[] num = new int[list.size()];
for(int i = 0;i<list.size();i++){
num[i] = list.get(i);
}
return num;
}
public void travelsal(TreeNode root,List<Integer> list){
if(root == null){
return;
}
if(root.left!= null){
travelsal(root.left,list);
}
list.add(root.val);
if(root.right!=null){
travelsal(root.right,list);
}
}
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
/**
* NC161 二叉树的中序遍历
* @author d3y1
*/
public class Solution {
private ArrayList<Integer> list = new ArrayList<>();
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
return solution1(root);
// return solution2(root);
}
/**
* 递归
* @param root
* @return
*/
private int[] solution1(TreeNode root){
inOrder(root);
int[] results = new int[list.size()];
for(int i=0; i<list.size(); i++){
results[i] = list.get(i);
}
return results;
}
/**
* 中序遍历(dfs)
* @param root
*/
private void inOrder(TreeNode root){
if(root == null){
return;
}
inOrder(root.left);
list.add(root.val);
inOrder(root.right);
}
/**
* 迭代(栈)
* @param root
* @return
*/
private int[] solution2(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
TreeNode curr;
while(root!=null || !stack.isEmpty()){
// 最左边
while(root != null){
stack.push(root);
root = root.left;
}
curr = stack.pop();
list.add(curr.val);
root = curr.right;
}
int[] results = new int[list.size()];
for(int i=0; i<list.size(); i++){
results[i] = list.get(i);
}
return results;
}
} public int[] inorderTraversal (TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
traversal(list,root);
int[] r = new int[list.size()];
for(int i=0;i<list.size();i++){
r[i] = list.get(i);
}
return r;
}
public void traversal (ArrayList<Integer> list,TreeNode node) {
if(node == null) return;
if(node.left != null) traversal(list,node.left);
list.add(node.val);
if(node.right != null) traversal(list,node.right);
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
// 1.初始化一个ArrayList用于动态添加数据
ArrayList<Integer> resultList = new ArrayList<>();
// 2.调用中序遍历递归函数
process(resultList, root);
// 3.将ArrayList转换成int[]并返回
return listToArray(resultList);
}
// 中序遍历递归函数
public void process(ArrayList<Integer> resultList, TreeNode root) {
if (root == null) {
return;
}
process(resultList, root.left);
resultList.add(root.val);
process(resultList, root.right);
}
// ArrayList转int[]
public int[] listToArray(ArrayList<Integer> resultList) {
int[] resultArray = new int[resultList.size()];
for (int i = 0; i < resultArray.length; i++) {
resultArray[i] = resultList.get(i);
}
return resultArray;
}
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
public int[] inorderTraversal (TreeNode root) {
// write code here
if(root==null){
return new int[0];
}
List<Integer> ans=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
while(root!=null || !stack.isEmpty()){
while(root!=null){
stack.push(root);
root=root.left;
}
TreeNode node=stack.pop();
ans.add(node.val);
root=node.right;
}
return ans.stream().mapToInt(Integer::intValue).toArray();
}
}
public class Solution {
/**
* 给定一个二叉树的根节点root,返回它的中序遍历结果。
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
if (null == root) {
return new int[] {};
}
Stack<TreeNode> stack = new Stack<>();
List<Integer> nums = new ArrayList<>();
pushAllLeft(root, stack);
while (!stack.isEmpty()) {
TreeNode top = stack.pop();
nums.add(top.val);
//如果出栈的节点是非叶子节点,将其标记为root节点,并将将root节点及其孩子的左节点加入
if (top.right != null) {
root = top.right;
pushAllLeft(root, stack);
}
}
return list2Array(nums);
}
/**
* 将root节点及其孩子的左节点加入栈
*
*
* @param root TreeNode类
* @param stack Stack<TreeNode>
* @return int整型一维数组
*/
public void pushAllLeft(TreeNode root, Stack<TreeNode> stack) {
TreeNode curr = root;
while (curr != null) {
stack.push(curr);
curr = curr.left;
}
}
/**
* List<Integer>转化为int[]
*
*
* @param nums List<Integer>类
* @return int整型一维数组
*/
public int[] list2Array(List<Integer> nums) {
int[] numArray = new int[nums.size()];
for (int i = 0; i < nums.size(); i++) {
numArray[i] = nums.get(i);
}
return numArray;
}
} 深度优先搜索-递归
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
List<Integer> list = new ArrayList<>();
dfs(root,list);
int[] res = new int[list.size()];
int cnt = 0;
for(Integer i:list){
res[cnt++] = i;
}
return res;
}
public void dfs(TreeNode root,List<Integer> list){
if(root==null) return;
dfs(root.left,list);
list.add(root.val);
dfs(root.right,list);
}
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
List<Integer> list = new ArrayList<>();
public int[] inorderTraversal (TreeNode root) {
if (root != null) {
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
}
int[] res=new int[list.size()];
for(int i =0;i<list.size();i++){
res[i]=list.get(i);
}
return res;
}
// public int[] inorderTraversal (TreeNode root) {
// // write code here
// List<Integer> list=new ArrayList<>();
// Stack<TreeNode> stack=new Stack<>();
// if(root==null){
// return new int[0];
// }
// TreeNode cur=root;
// while(!stack.isEmpty()||cur!=null){
// while(cur!=null){
// stack.push(cur);
// cur=cur.left;
// }
// if(!stack.isEmpty()){
// TreeNode temp=stack.pop();
// list.add(temp.val) ;
// cur=temp.right;
// }
// }
// int[] res=new int[list.size()];
// for(int i =0;i<list.size();i++){
// res[i]=list.get(i);
// }
// return res;
// }
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public List<Integer> list=new ArrayList<Integer>();
public int[] inorderTraversal (TreeNode root) {
// write code here
order(root);
int[] arr=new int[list.size()];
for(int i=0;i<list.size();i++){
arr[i]=list.get(i);
}
return arr;
}
public void order(TreeNode node){
if(node==null) return;
order(node.left);
list.add(node.val);
order(node.right);
}
}
import java.util.*;
public class Solution {
public int[] inorderTraversal (TreeNode root) {
List<Integer> list = new ArrayList<>();
inOrder(root,list);
int[] res = new int[list.size()];
for(int i=0;i<list.size();i++){
res[i] = list.get(i);
}
return res;
}
public static void inOrder(TreeNode root,List<Integer> list){
if(root != null) {
inOrder(root.left,list);
list.add(root.val);
inOrder(root.right,list);
}
}
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
ArrayList<Integer> list = new ArrayList<>();
inorder(list, root);
int[] mylist = new int[list.size()];
for(int i=0;i<list.size();i++){
mylist[i] = list.get(i);
}
return mylist;
}
public void inorder(ArrayList<Integer> list, TreeNode root){
if(root == null){
return;
}
inorder(list, root.left);
list.add(root.val);
inorder(list, root.right);
}
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
if (root == null) {
return new int[0];
}
List<Integer> list = new ArrayList<Integer>();
traversal(root, list);
return list.stream().mapToInt(i->i).toArray();
}
private void traversal(TreeNode root, List<Integer> list) {
if(root.left != null) {
traversal(root.left,list);
}
list.add(root.val);
if(root.right != null) {
traversal(root.right,list);
}
}
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
if(root == null){ return new int[0]; }
// 中序遍历: left -> this -> right
// 存储中序遍历的结果
List<Integer> arrayList = new ArrayList();
// 中序遍历
inDFS(root, arrayList);
// 转int数组
return arrayList.stream().mapToInt(i->i).toArray();
}
public void inDFS (TreeNode root, List<Integer> arrayList) {
// 节点空,返回
if(root == null){
return;
}
// 向左遍历
inDFS (root.left, arrayList);
// 存储当前节点
arrayList.add(root.val);
// 向右遍历
inDFS (root.right, arrayList);
}
} public int[] inorderTraversal (TreeNode root) {
// write code here
List<TreeNode> resultList = new ArrayList();
readTree(root,resultList);
int size = resultList.size();
int[] result = new int[size];
for(int i = 0;i<size; i++) {
result[i] = resultList.get(i).val;
}
return result;
}
private void readTree(TreeNode root,List<TreeNode> resultList) {
if(null == root) {
return ;
}
TreeNode leftNode = root.left;
readTree(leftNode,resultList);
if(null != root) {
resultList.add(root);
}
TreeNode rightNode = root.right;
readTree(rightNode,resultList);
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
if (root == null) return new int[0];
ArrayList<Integer> list = new ArrayList();
Stack<TreeNode> stack = new Stack();
while (!stack.isEmpty() || root != null) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
TreeNode node = stack.pop();
list.add(node.val);
root = node.right;
}
}
int[] res = new int[list.size()];
for (int i = 0; i < res.length; i ++) {
res[i] = list.get(i);
}
return res;
}
} //morris
public int[] inorderTraversal (TreeNode root) {
// write code here
List<Integer> list = new ArrayList<>();
TreeNode pre = null;
while(root != null){
if(root.left != null){
pre = root.left;
while(pre.right != null && pre.right != root){
pre = pre.right;
}
if(pre.right == null){
pre.right = root;
root = root.left;
}else{
pre.right = null;
list.add(root.val);
root = root.right;
}
}else{
list.add(root.val);
root = root.right;
}
}
int[] ans = new int[list.size()];
for(int i = 0; i<list.size();i++){
ans[i] = list.get(i);
}
return ans;
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
ArrayList<Integer> l = new ArrayList<>();
public int[] inorderTraversal (TreeNode root) {
// write code here
inorder(root);
int[] r = new int[l.size()];
for(int i = 0; i < l.size(); i++){
r[i] = l.get(i);
}
return r;
}
public void inorder(TreeNode root){
if(root != null){
//LDR
inorder(root.left);
l.add(root.val);
inorder(root.right);
}
}
}