给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
数据范围:二叉树的节点数量满足 ,二叉树节点的值满足 ,树的各节点的值各不相同
示例 1:
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[] preorderTraversal (TreeNode root) { // write code here ArrayList<Integer> resultList = new ArrayList<Integer>(); tran(root, resultList); int[] ints = new int[resultList.size()]; for (int i = 0; i < resultList.size(); i++) { ints[i] = resultList.get(i); } return ints; } public void tran(TreeNode root, ArrayList<Integer> resultList) { if (root != null) { resultList.add(root.val); tran(root.left, resultList); tran(root.right, resultList); } } }
public class Solution { /** * 给你二叉树的根节点 root ,返回它节点值的 前序 遍历 * * * @param root TreeNode类 * @return int整型一维数组 */ public int[] preorderTraversal (TreeNode root) { // write code here if (null == root) { return new int[] {}; } Stack<TreeNode> stack = new Stack<>(); List<Integer> numList = new ArrayList<Integer>(); stack.push(root); while (!stack.isEmpty()) { root = stack.pop();//顶部出栈 numList.add(root.val);//加入列表 if (root.right != null) { stack.push(root.right); } if (root.left != null) { stack.push(root.left); } } return list2Array(numList); } /** * 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 int[] preorderTraversal (TreeNode root) { // write code here if(root == null){ return new int[0]; } Stack<TreeNode> stack = new Stack<>(); List<Integer> list = new ArrayList<>(); stack.push(root); while(!stack.empty()){ TreeNode node = stack.pop(); list.add(node.val); if(node.right != null){ stack.push(node.right); } if(node.left != null){ stack.push(node.left); } } int[] result = new int[list.size()]; for(int i = 0 ; i < list.size() ; i++){ result[i] = list.get(i); } return result; }
public int[] preorderTraversal (TreeNode root) { //先序遍历 头左右 List<Integer> list=new ArrayList<>(); build(root,list); int[] ans=new int[list.size()]; for(int i=0;i<list.size();i++){ ans[i]=list.get(i); } return ans; } public void build(TreeNode root,List<Integer> list){ if(root==null){ return ; } list.add(root.val); build(root.left,list); build(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 { public int[] preorderTraversal (TreeNode root) { List<Integer> list = new ArrayList<Integer>(); preOrder(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 preOrder(TreeNode root,List<Integer> list){ if(root!=null){ list.add(root.val); preOrder(root.left,list); preOrder(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[] preorderTraversal (TreeNode root) { // write code here ArrayList<Integer> list = new ArrayList<>(); preorder(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 preorder(ArrayList<Integer> list, TreeNode root){ if(root == null){ return; } list.add(root.val); preorder(list,root.left); preorder(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[] preorderTraversal (TreeNode root) { // write code here if (root == null) { return new int[0]; } List<Integer> list = new ArrayList<Integer>(); traversal(root, list); int[] array = new int[list.size()]; for(int i = 0; i< list.size();i++) { array[i] = list.get(i); } return array; } private void traversal(TreeNode root, List<Integer> list) { list.add(root.val); if(root.left != null) { traversal(root.left,list); } if(root.right != null) { traversal(root.right,list); } } }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型一维数组 */ public int[] preorderTraversal (TreeNode root) { // 前序遍历: this -> left -> right // 存储前序遍历的结果 List<Integer> arrayList = new ArrayList<Integer>(); // 前序遍历 dfs(root, arrayList); // 转int数组 return arrayList.stream().mapToInt(i->i).toArray(); } public void dfs(TreeNode root, List<Integer> arrayList){ // 等于空不进行遍历 if (root == null){ return; } // 存储当前节点 arrayList.add(root.val); // 向左子树遍历 dfs(root.left,arrayList); // 向右子树遍历 dfs(root.right,arrayList); } }
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[] preorderTraversal (TreeNode root) { // write code here List list = new ArrayList<Integer>(); preorderImpl(root,list); return convertIntegers(list); // 先根节点 } public void preorderImpl(TreeNode root, List list){ // 特殊情况 if( root == null){ return ; } // 将根节点的值插入到列表 list.add(root.val); //将root的左子节点插入到list if(root.left != null){ preorderImpl(root.left, list); } //将root的右子节点插入到list if(root.right != null){ preorderImpl(root.right, list); } } public int[] convertIntegers(List<Integer> integers) { int[] ret = new int[integers.size()]; for (int i=0; i < ret.length; i++) { ret[i] = integers.get(i).intValue(); } return ret; } }
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[] preorderTraversal (TreeNode root) { // write code here preorder(root); int[] r = new int[l.size()]; for(int i = 0; i < l.size(); i++){ r[i] = l.get(i); } return r; } public void preorder(TreeNode root){ if(root != null){ //DLR l.add(root.val); preorder(root.left); preorder(root.right); } } }
public void preOrder1(TreeNode root) { if(root==null) return; // 栈,保存访问过的节点 LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); TreeNode node = root; while(!stack.isEmpty() || node!=null) { // 一直往左下遍历 while(node!=null) { stack.push(node); nodesVal.add(node.val); node = node.left; } // 无左子树的时候,去判断右子树 node = stack.pop(); node = 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[] preorderTraversal (TreeNode root) { // write code here if(root == null) return new int[0]; ArrayList<Integer> res = new ArrayList(); Stack<TreeNode> stack = new Stack(); stack.push(root); while (!stack.isEmpty()) { TreeNode node = stack.pop(); res.add(node.val); if (node.right != null) { stack.push(node.right); } if (node.left != null) { stack.push(node.left); } } int[] nums = new int[res.size()]; for (int i = 0; i < res.size(); i ++) { nums[i] = res.get(i); } return nums; } }
public class Solution { public int[] preorderTraversal (TreeNode root) { List<Integer> list = new ArrayList(); preorder(root, list); return list.stream().mapToInt(Integer::valueOf).toArray(); } public void preorder(TreeNode root, List<Integer> list) { if (root == null) return; list.add(root.val); preorder(root.left, list); preorder(root.right, list); } }