路径/Path Sum
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
if(root == null) return res;
helper(root, res, new StringBuilder());
return res;
}
private void helper(TreeNode root, List<String> res, StringBuilder sb){
if(root == null){
return;
}
int len = sb.length();
sb.append(root.val);
if(root.left == null && root.right == null){
res.add(sb.toString());
}else{
sb.append("->");
helper(root.left, res, sb);
helper(root.right, res, sb);
}
sb.setLength(len);
}
}Path Sum II (好题)
Path Sum III
Path Sum IV
Sum root to leaf numbers
Sum of left leaves
Longest Univalue Path
Second minimum node in a Binary Tree
All Nodes Distance K in Binary Tree
Last updated