赞
踩
目录
Java使用Collections.reverse()反转一个List
- public class TreeNode {
- int val;
- TreeNode left;
- TreeNode right;
- TreeNode() {}
- TreeNode(int val) { this.val = val; }
- TreeNode(int val, TreeNode left, TreeNode right) {
- this.val = val;
- this.left = left;
- this.right = right;
- }
- }
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- public List<Integer> preorderTraversal(TreeNode root) {
- LinkedList<Integer> result=new LinkedList<>();
- preorder(root,result);
- return result;
- }
- public void preorder(TreeNode root,LinkedList<Integer> result)
- {
- if(root==null)
- {
- return;
- }
- result.add(root.val);
- preorder(root.left,result);
- preorder(root.right,result);
- }
- }
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- public List<Integer> inorderTraversal(TreeNode root) {
- List<Integer> result=new LinkedList<>();
- inorder(root,result);
- return result;
- }
- public void inorder(TreeNode root,List<Integer> result)
- {
- if(root==null)
- {
- return;
- }
- inorder(root.left,result);
- result.add(root.val);
- inorder(root.right,result);
- }
- }
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- public List<Integer> postorderTraversal(TreeNode root) {
- List<Integer>result=new ArrayList<>();
- postorder(root,result);
- return result;
- }
- public void postorder(TreeNode root,List<Integer>result)
- {
- if(root==null)
- return;
- postorder(root.left,result);
- postorder(root.right,result);
- result.add(root.val);
- }
- }
List接口是Collection接口的子接口,List有一个重要的实现类--ArrayList类,List中的元素是有序排列的而且可重复,所以被称为是序列。
List可以精确的控制每个元素的插入位置,或删除某个位置元素,它的实现类ArrayList底层是由数组实现的。
List中有增删改查的方法。
所有已知实现类:
AbstractList
, AbstractSequentialList
, ArrayList
, AttributeList
, CopyOnWriteArrayList
, LinkedList
, RoleList
, RoleUnresolvedList
, Stack
, Vector
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- public List<Integer> preorderTraversal(TreeNode root) {
- List<Integer> result = new ArrayList<>();
- if (root == null){
- return result;
- }
- Stack<TreeNode> stack = new Stack<>();
- stack.push(root);
- while (!stack.isEmpty()){
- TreeNode node = stack.pop();
- result.add(node.val);
- if (node.right != null){
- stack.push(node.right);
- }
- if (node.left != null){
- stack.push(node.left);
- }
- }
- return result;
- }
- }
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- public List<Integer> postorderTraversal(TreeNode root) {
- List<Integer> result=new ArrayList<>();
- if(root==null)
- {
- return result;
- }
- Stack<TreeNode>stack=new Stack<>();
- stack.push(root);
- while(!stack.isEmpty())
- {
- TreeNode node=stack.pop();
- result.add(node.val);
- if(node.left!=null)
- {
- stack.push(node.left);
- }
- if(node.right!=null)
- {
- stack.push(node.right);
- }
- }
- Collections.reverse(result);
- return result;
- }
- }
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- public List<Integer> inorderTraversal(TreeNode root) {
- List<Integer>result=new ArrayList<>();
- if(root==null)
- {
- return result;
- }
- Stack<TreeNode>stack=new Stack<>();
- TreeNode cur=root;
- while(cur!=null||!stack.isEmpty())
- {
- if(cur!=null)
- {
- stack.push(cur);
- cur=cur.left;
- }
- else
- {
- cur=stack.pop();
- result.add(cur.val);
- cur=cur.right;
- }
- }
- return result;
- }
- }
只要不是空结点,就一路向左入栈,是空结点,就弹出当前结点,并且放入返回结果中。继续判断右节点是不是为空,如果为空,就继续弹出刚刚访问过的结点。 在使用迭代法写中序遍历,就需要借用指针的遍历来帮助访问节点,栈则用来处理节点上的元素。
二刷的时候再看
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。