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 {
private List<Integer> result = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if (root == null) return this.result;
inOrder(root);
return this.result;
}
public void inOrder(TreeNode root){
if (root == null) return;
inOrder(root.left);
this.result.add(root.val);
inOrder(root.right);
}
}
java - 94. 二叉树的中序遍历 - 递归实现
于 2022-03-30 16:04:35 首次发布