给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
输入: 1 / \ 2 3 \ 5 输出: ["1->2->5", "1->3"] 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
深度优先搜索+路径记录。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def binaryTreePaths(self, root: TreeNode) -> List[str]:
def dfs(root):
if root is None:
return
t.append(str(root.val))
if root.left is None and root.right is None:
ans.append('->'.join(t))
dfs(root.left)
dfs(root.right)
t.pop()
t = []
ans = []
dfs(root)
return ans
/**
* Definition for a binary tree node.
* public 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<String> ans;
private List<String> t;
public List<String> binaryTreePaths(TreeNode root) {
ans = new ArrayList<>();
t = new ArrayList<>();
dfs(root);
return ans;
}
private void dfs(TreeNode root) {
if (root == null) {
return;
}
t.add(root.val + "");
if (root.left == null && root.right == null) {
ans.add(String.join("->", t));
}
dfs(root.left);
dfs(root.right);
t.remove(t.size() - 1);
}
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function binaryTreePaths(root: TreeNode | null): string[] {
let ans = [];
let t = [];
function dfs(root) {
if (!root) return;
t.push(String(root.val));
if (!root.left && !root.right) ans.push(t.join('->'));
dfs(root.left);
dfs(root.right);
t.pop();
}
dfs(root);
return ans;
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func binaryTreePaths(root *TreeNode) []string {
var ans []string
var t []string
var dfs func(root *TreeNode)
dfs = func(root *TreeNode) {
if root == nil {
return
}
t = append(t, strconv.Itoa(root.Val))
if root.Left == nil && root.Right == nil {
ans = append(ans, strings.Join(t, "->"))
}
dfs(root.Left)
dfs(root.Right)
t = t[:len(t)-1]
}
dfs(root)
return ans
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<string> ans;
vector<string> binaryTreePaths(TreeNode* root) {
dfs(root, "");
return ans;
}
void dfs(TreeNode* root, string t) {
t += to_string(root->val);
if (!root->left && !root->right)
{
ans.push_back(t);
return;
}
t += "->";
if (root->left) dfs(root->left, t);
if (root->right) dfs(root->right, t);
}
};