comments | difficulty | edit_url | tags | |||||
---|---|---|---|---|---|---|---|---|
true |
简单 |
|
给你一个二叉树的根节点 root
,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [1,2,3,null,5] 输出:["1->2->5","1->3"]
示例 2:
输入:root = [1] 输出:["1"]
提示:
- 树中节点的数目在范围
[1, 100]
内 -100 <= Node.val <= 100
我们可以使用深度优先搜索的方法遍历整棵二叉树,每一次我们将当前的节点添加到路径中。如果当前的节点是叶子节点,则我们将整个路径加入到答案中。否则我们继续递归遍历节点的孩子节点。最后当我们递归结束返回到当前节点时,我们需要将当前节点从路径中删除。
时间复杂度
# 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: Optional[TreeNode]) -> List[str]:
def dfs(root: Optional[TreeNode]):
if root is None:
return
t.append(str(root.val))
if root.left is None and root.right is None:
ans.append("->".join(t))
else:
dfs(root.left)
dfs(root.right)
t.pop()
ans = []
t = []
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 = new ArrayList<>();
private List<String> t = new ArrayList<>();
public List<String> binaryTreePaths(TreeNode root) {
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));
} else {
dfs(root.left);
dfs(root.right);
}
t.remove(t.size() - 1);
}
}
/**
* 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> binaryTreePaths(TreeNode* root) {
vector<string> ans;
vector<string> t;
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
if (!root) {
return;
}
t.push_back(to_string(root->val));
if (!root->left && !root->right) {
ans.push_back(join(t));
} else {
dfs(root->left);
dfs(root->right);
}
t.pop_back();
};
dfs(root);
return ans;
}
string join(vector<string>& t, string sep = "->") {
string ans;
for (int i = 0; i < t.size(); ++i) {
if (i > 0) {
ans += sep;
}
ans += t[i];
}
return ans;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func binaryTreePaths(root *TreeNode) (ans []string) {
t := []string{}
var dfs func(*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, "->"))
} else {
dfs(root.Left)
dfs(root.Right)
}
t = t[:len(t)-1]
}
dfs(root)
return
}
/**
* 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[] {
const ans: string[] = [];
const t: number[] = [];
const dfs = (root: TreeNode | null) => {
if (!root) {
return;
}
t.push(root.val);
if (!root.left && !root.right) {
ans.push(t.join('->'));
} else {
dfs(root.left);
dfs(root.right);
}
t.pop();
};
dfs(root);
return ans;
}