给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
数据范围:二叉树的节点数量满足
,二叉树节点的值满足
,树的各节点的值各不相同
示例 1:
public int[] preorderTraversal (TreeNode root) {
// write code here
if (root == null)
{
return new int[]{};
}
ArrayList<Integer> list = new ArrayList<>();
pre(root, list);
return list.stream().filter(integer -> integer != null).mapToInt(i->i).toArray();
}
public void pre (TreeNode root, ArrayList arr)
{
arr.add(root.val);
if (root.left != null)
{
pre(root.left, arr);
}
if (root.right != null)
{
pre(root.right, arr);
}
} public int[] preorderTraversal1 (TreeNode root) {
if(root == null) return new int[0];
ArrayList<Integer> list = new ArrayList<>();
traversal(root,list);
return list.stream().mapToInt(Integer::intValue).toArray();
}
public void traversal(TreeNode node,ArrayList<Integer> list){
list.add(node.val);
if(node.left != null) traversal(node.left,list);
if(node.right != null) traversal(node.right,list);
}
// 栈
public int[] preorderTraversal (TreeNode root) {
if(root == null) return new int[0];
ArrayList<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
list.add(node.val);
if(node.right != null) stack.push(node.right);
if(node.left != null) stack.push(node.left);
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
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
// 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;
}
resultList.add(root.val);
process(resultList, root.left);
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 {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @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;
}
}