?LeetCode刷題實(shí)戰(zhàn)257:二叉樹的所有路徑
Given the root of a binary tree, return all root-to-leaf paths in any order.
A leaf is a node with no children.

解題

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> list = new LinkedList<>();
helper(root, "", list);
return list;
}
private void helper(TreeNode root, String s, List<String>list){
if (root == null){
return;
}
s = s + root.val;
if (root.left == null && root.right == null){
list.add(s);
return;
}
if (root.left != null){
helper(root.left, s + "->", list);
}
if (root.right != null){
helper(root.right, s + "->", list);
}
}
}
LeetCode1-240題匯總,希望對你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)241:為運(yùn)算表達(dá)式設(shè)計(jì)優(yōu)先級(jí)
LeetCode刷題實(shí)戰(zhàn)242:有效的字母異位詞
LeetCode刷題實(shí)戰(zhàn)243:最短單詞距離
LeetCode刷題實(shí)戰(zhàn)244:最短單詞距離 II
LeetCode刷題實(shí)戰(zhàn)245:最短單詞距離 III
LeetCode刷題實(shí)戰(zhàn)246:中心對稱數(shù)
LeetCode刷題實(shí)戰(zhàn)247:中心對稱數(shù)II
LeetCode刷題實(shí)戰(zhàn)248:中心對稱數(shù)III
LeetCode刷題實(shí)戰(zhàn)249:移位字符串分組
LeetCode刷題實(shí)戰(zhàn)250:統(tǒng)計(jì)同值子樹
LeetCode刷題實(shí)戰(zhàn)251:展開二維向量
LeetCode刷題實(shí)戰(zhàn)252:會(huì)議室
LeetCode刷題實(shí)戰(zhàn)253:會(huì)議室II
LeetCode刷題實(shí)戰(zhàn)254:因子的組合
