给定一个二叉树的根节点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 { 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); } } }
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> visited=new ArrayList<Integer>(); inorder(root,visited); int[] array=new int[visited.size()]; for(int i=0;i<=visited.size()-1;i++){ array[i]=visited.get(i); } return array; } public void inorder(TreeNode root,List<Integer> visited){ if(root==null){return;} inorder(root.left,visited); visited.add(root.val); inorder(root.right,visited); } }
非递归前中后序使用同一套模板:
前序遍历:
import java.util.*; public class Solution { public int[] inorderTraversal (TreeNode root) { if(root == null) return new int[0]; Stack<NodeWithStatus> st = new Stack<NodeWithStatus>(); st.push(new NodeWithStatus(root,1)); List<Integer> list = new ArrayList<>(); while(!st.isEmpty()){ NodeWithStatus nws = st.peek(); if(nws.status == 1){ //前序遍历 list.add(nws.node.val); if(nws.node.left != null){ NodeWithStatus nodeWithStatus = new NodeWithStatus(nws.node.left,1); st.push(nodeWithStatus); } nws.status = 2; }else if(nws.status == 2){ if(nws.node.right != null){ NodeWithStatus nodeWithStatus = new NodeWithStatus(nws.node.right,1); st.push(nodeWithStatus); } nws.status = 3; }else if(nws.status == 3){ st.pop(); } } int size = list.size(); int[] res = new int[size]; for(int i = 0; i < size; i++){ res[i] = list.get(i); } return res; } } class NodeWithStatus{ TreeNode node; int status; public NodeWithStatus(TreeNode node,int status){ this.node = node; this.status = status; } }
中序遍历
import java.util.*; public class Solution { public int[] inorderTraversal (TreeNode root) { if(root == null) return new int[0]; Stack<NodeWithStatus> st = new Stack<NodeWithStatus>(); st.push(new NodeWithStatus(root,1)); List<Integer> list = new ArrayList<>(); while(!st.isEmpty()){ NodeWithStatus nws = st.peek(); if(nws.status == 1){ if(nws.node.left != null){ NodeWithStatus nodeWithStatus = new NodeWithStatus(nws.node.left,1); st.push(nodeWithStatus); } nws.status = 2; }else if(nws.status == 2){ //中序遍历 list.add(nws.node.val); if(nws.node.right != null){ NodeWithStatus nodeWithStatus = new NodeWithStatus(nws.node.right,1); st.push(nodeWithStatus); } nws.status = 3; }else if(nws.status == 3){ st.pop(); } } int size = list.size(); int[] res = new int[size]; for(int i = 0; i < size; i++){ res[i] = list.get(i); } return res; } } class NodeWithStatus{ TreeNode node; int status; public NodeWithStatus(TreeNode node,int status){ this.node = node; this.status = status; } }
后续遍历:
import java.util.*; public class Solution { public int[] inorderTraversal (TreeNode root) { if(root == null) return new int[0]; Stack<NodeWithStatus> st = new Stack<NodeWithStatus>(); st.push(new NodeWithStatus(root,1)); List<Integer> list = new ArrayList<>(); while(!st.isEmpty()){ NodeWithStatus nws = st.peek(); if(nws.status == 1){ if(nws.node.left != null){ NodeWithStatus nodeWithStatus = new NodeWithStatus(nws.node.left,1); st.push(nodeWithStatus); } nws.status = 2; }else if(nws.status == 2){ if(nws.node.right != null){ NodeWithStatus nodeWithStatus = new NodeWithStatus(nws.node.right,1); st.push(nodeWithStatus); } nws.status = 3; }else if(nws.status == 3){ //后序遍历 list.add(nws.node.val); st.pop(); } } int size = list.size(); int[] res = new int[size]; for(int i = 0; i < size; i++){ res[i] = list.get(i); } return res; } } class NodeWithStatus{ TreeNode node; int status; public NodeWithStatus(TreeNode node,int status){ this.node = node; this.status = status; } }
List<Integer> list= new ArrayList<>(); public int[] inorderTraversal(TreeNode root) { // write code here if(root ==null) return new int[0]; // TreeNode downNode =root.left; getList(root); return list.stream().mapToInt(Integer::valueOf).toArray(); } public void getList(TreeNode root) { // write code here if(root.left !=null) { getList(root.left); } list.add(root.val); if(root.right !=null){ getList(root.right); } }