求给定的二叉树的前序遍历。
例如:
给定的二叉树为{1,#,2,3},
返回:[1,2,3].
备注;用递归来解这道题很简单,你可以给出迭代的解法么?
如果你不明白{1,#,2,3}的含义,点击查看相关信息
public class Solution { public ArrayList<Integer> preorderTraversal (TreeNode root) { // write code here ArrayList<Integer> list = new ArrayList<>(); if (root == null) return list; LinkedList<TreeNode> treeNodes = new LinkedList<>(); TreeNode prev = null; while(root!=null || !treeNodes.isEmpty()){ //将左节点进栈 while (root!= null){ treeNodes.push(root); list.add(root.val); root = root.left; } root = treeNodes.pop(); if (root.right ==null || root.right == prev){ prev = root; root = null; }else { root = root.right; } } return list; } }
public ArrayList<Integer> preorderTraversal (TreeNode root) { if(root==null){ return null; } ArrayList<Integer> result=new ArrayList(); // write code here Stack<TreeNode> stack=new Stack(); stack.push(root); while(!stack.empty()){ TreeNode pop=stack.pop(); result.add(pop.val); TreeNode right=pop.right; if(right!=null){ stack.push(right); } TreeNode left=pop.left; if(left!=null){ stack.push(left); } } return result; }
import java.util.ArrayList; import java.util.List; /** * @program: arask * @description: 编程测试 * @author: fubowen * @create: 2020-10-10 15:21 **/ public class Solution { /** * * @param root TreeNode类 * @return int整型ArrayList */ public ArrayList<Integer> preorderTraversal (TreeNode root) { // write code here //root为空 不为空 //先根 左子树 右子树 ArrayList<Integer> list=new ArrayList<>(); if(root!=null){ list.add(root.val); ArrayList<Integer> leftList=preorderTraversal(root.left); list.addAll(leftList); ArrayList<Integer> rightList=preorderTraversal(root.right); list.addAll(rightList); } return list; } }
import java.util.*; public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { //后进先出栈 ArrayList<Integer> al = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); if(root!=null){ stack.push(root); while(!stack.isEmpty()){ //1、根 TreeNode tmp = stack.pop(); al.add(tmp.val); //2、先入后出,右边子树在后 if(tmp.right!=null){ stack.push(tmp.right); } //2、**先出,左边子树在前 if(tmp.left!=null){ stack.push(tmp.left); } } } return al; } }
import java.util.ArrayList; import java.util.Stack; public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { Stack<TreeNode> stack = new Stack<TreeNode>(); ArrayList<Integer> list = new ArrayList<Integer>(); if(root != null){ stack.push(root); } while(stack.size() > 0){ if(stack.peek() != null){ list.add(stack.peek().val); stack.push(stack.peek().left); }else{ stack.pop(); if(stack.size() > 0 && stack.peek() != null){ stack.push(stack.pop().right); } } } return list; } }
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ import java.util.ArrayList; public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> list=new ArrayList<Integer>(); if(root==null) return list; function(list,root); return list; } public void function(ArrayList<Integer> list,TreeNode root){ if(root==null) return; list.add(root.val); if(root.left!=null){ function(list,root.left); } if(root.right!=null){ function(list,root.right); } } }
import java.util.*; public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<>(); if(root==null) return list; 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; } }
import java.util.Stack; import java.util.ArrayList; public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); if(root == null) return list; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while(!stack.isEmpty()){ root = stack.pop(); list.add(root.val); if(root.right != null) stack.push(root.right); if(root.left != null) stack.push(root.left); } return list; } }利用栈的思想,后进先出
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree{1,#,2,3},
1
\
2
/
3
return[1,2,3].
Note: Recursive solution is trivial, could you do it iteratively?
同上题,只需要稍微换一下位置即可,十分方便。
import java.util.*;
class Command{
public String str;
public TreeNode node;
public Command(String str,TreeNode node){
this.str = str;
this.node = node;
}
}
public class Solution {
public ArrayList preorderTraversal(TreeNode root) {
ArrayList res = new ArrayList();
if(root == null){
return res;
}
Stack stack = new Stack();
stack.push(new Command("go",root));
while(!stack.isEmpty()){
Command c = stack.pop();
if(c.str == "print"){
res.add(c.node.val);
}else{
if(c.node.right != null){
stack.push(new Command("go",c.node.right));
}
if(c.node.left != null){
stack.push(new Command("go",c.node.left));
}
stack.push(new Command("print",c.node));
}
}
return res;
}
}
import java.util.Stack; import java.util.ArrayList; public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> s = new Stack<TreeNode>(); while(root!=null || !s.isEmpty()){ while(root!=null){ s.push(root.right); list.add(root.val); root = root.left; } root = s.pop(); } return list; } }
import java.util.*;//还是递归好用 public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> al = new ArrayList<Integer>(); if(root==null)return al; al.add(root.val); al.addAll(preorderTraversal(root.left)); al.addAll(preorderTraversal(root.right)); return al; } }