Open youngyangyang04 opened 3 months ago
python的写法里似乎遗漏了一句dfs(root)
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
traversal(root, result);
return result;
}
private void traversal(TreeNode node, List<Integer> res) {
if (node == null) return;
res.add(node.val);
traversal(node.left, res);
traversal(node.right, res);
}
}
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
traversal(root, result);
return result;
}
private void traversal(TreeNode node, List<Integer> res) {
if (node == null) return;
traversal(node.left, res);
res.add(node.val);
traversal(node.right, res);
}
}
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
traversal(root, result);
return result;
}
private void traversal(TreeNode node, List<Integer> res) {
if (node == null) return;
traversal(node.left, res);
traversal(node.right, res);
res.add(node.val);
}
}
没急,别预判我
https://www.programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html