94. Binary Tree Inorder Traversal

来源:互联网 发布:js对象数组转json 编辑:程序博客网 时间:2024/06/08 04:54
/** * 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) {        List<Integer> ans = new LinkedList<Integer>();            if(root!=null){              in_order(ans,root.left);              ans.add(root.val);              in_order(ans,root.right);          }          return ans;      }    public void in_order(List<Integer> ans,TreeNode root){          if(root != null){              in_order(ans,root.left);              ans.add(root.val);              in_order(ans,root.right);          }      }      }

原创粉丝点击