Algorithms-94.Binary Tree Inorder Traversal

来源:互联网 发布:帝国cms企业网站模板 编辑:程序博客网 时间:2024/06/17 10:05
思路:中序遍历,递归,按照定义来做。

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public List<Integer> inorderTraversal(TreeNode root) {    return inorder(root,new ArrayList<Integer>());    }    public List<Integer> inorder(TreeNode root,List<Integer> list){    if (root!=null) {list=inorder(root.left,list);    list.add(root.val);list=inorder(root.right,list);}    return list;    }}


耗时:292ms,中游。


0 0
原创粉丝点击