二分搜索树:
不了解的可以看这:
二分搜索树(Java)-CSDN博客
中序遍历:
public List<Integer> inorderTraversal(TreeNode root) {List<Integer>list=new ArrayList<>();midPai(root,list);return list;}public void midPai(TreeNode root,List<Integer>list){if(root==null){return ;}midPai(root.left,list);list.add(root.val);midPai(root.right,list);}
后序遍历:
public List<Integer> inorderTraversal(TreeNode root) {List<Integer>list=new ArrayList<>();midPai(root,list);return list;}public void midPai(TreeNode root,List<Integer>list){if(root==null){return ;}midPai(root.left,list);midPai(root.right,list);list.add(root.val);}
先序遍历:
public List<Integer> inorderTraversal(TreeNode root) {List<Integer>list=new ArrayList<>();midPai(root,list);return list;}public void midPai(TreeNode root,List<Integer>list){if(root==null){return ;}list.add(root.val);midPai(root.left,list);midPai(root.right,list);}