/** * 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> res = new ArrayList<Integer>(); preorder(root,res); return res; } public void preorder(TreeNode root, List arr) { if (root == null) return; arr.add(root.val); preorder(root.left,arr); preorder(root.right,arr); } }
class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); inorder(res,root); return res; } public void inorder(List res, TreeNode root) { if (root == null) return; inorder(res,root.left); res.add(root.val); inorder(res,root.right); } }
class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); postorder(root,res); return res; } public void postorder(TreeNode root, List res) { if (root == null) return; postorder(root.left,res); postorder(root.right,res); res.add(root.val); } }
标签:right,TreeNode,val,res,中序,List,二叉树,root,前序 From: https://www.cnblogs.com/fulaien/p/16904926.html