标签:null lin span div image tor this his blank
AC代码:
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The root of binary tree. * @return: Preorder in ArrayList which contains node values. */ public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> res=new ArrayList<>(); if(root==null) return res; res.add(root.val); res.addAll(preorderTraversal(root.left)); res.addAll(preorderTraversal(root.right)); return res; } }
题目来源: http://www.lintcode.com/zh-cn/problem/binary-tree-preorder-traversal/
AC代码:
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The root of binary tree. * @return: Inorder in ArrayList which contains node values. */ public ArrayList<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> res=new ArrayList<>(); if(root==null) return res; res.addAll(inorderTraversal(root.left)); res.add(root.val); res.addAll(inorderTraversal(root.right)); return res; } }
题目来源: http://www.lintcode.com/zh-cn/problem/binary-tree-inorder-traversal/#
AC代码:
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The root of binary tree. * @return: Postorder in ArrayList which contains node values. */ public ArrayList<Integer> postorderTraversal(TreeNode root) { ArrayList<Integer> res=new ArrayList<>(); if(root==null) return res; res.addAll(postorderTraversal(root.left)); res.addAll(postorderTraversal(root.right)); res.add(root.val); return res; } }
题目来源: http://www.lintcode.com/zh-cn/problem/binary-tree-postorder-traversal/#
lintcode 66.67.68 二叉树遍历(前序、中序、后序)
标签:null lin span div image tor this his blank
原文地址:http://www.cnblogs.com/cc11001100/p/6361880.html