Skip to content

Commit 5db18b5

Browse files
committed
solve problem Binary Tree Paths
1 parent 9b9a068 commit 5db18b5

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ All solutions will be accepted!
5959
|118|[Pascals Triangle](https://leetcode-cn.com/problems/pascals-triangle/description/)|[java/py/js](./algorithms/PascalsTriangle)|Easy|
6060
|303|[Range Sum Query Immutable](https://leetcode-cn.com/problems/range-sum-query-immutable/description/)|[java/py/js](./algorithms/RangeSumQueryImmutable)|Easy|
6161
|412|[Fizz Buzz](https://leetcode-cn.com/problems/fizz-buzz/description/)|[java/py/js](./algorithms/FizzBuzz)|Easy|
62+
|257|[Binary Tree Paths](https://leetcode-cn.com/problems/binary-tree-paths/description/)|[java/py/js](./algorithms/BinaryTreePaths)|Easy|
6263

6364
# Database
6465
|#|Title|Solution|Difficulty|

algorithms/BinaryTreePaths/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Binary Tree Paths
2+
This problem is easy to solve
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public List<String> binaryTreePaths(TreeNode root) {
12+
List<String> res = new ArrayList<String>();
13+
List<Map<String, Object>> paths = new ArrayList<Map<String, Object>>();
14+
15+
if (root == null) return res;
16+
17+
Map<String, Object> rootPath = new HashMap<String, Object>();
18+
rootPath.put("node", root);
19+
rootPath.put("path", String.valueOf(root.val));
20+
paths.add(rootPath);
21+
22+
while (paths.size() > 0) {
23+
Map<String, Object> nodePath = paths.get(paths.size() - 1);
24+
paths.remove(paths.size() - 1);
25+
TreeNode node = (TreeNode) nodePath.get("node");
26+
String path = (String) nodePath.get("path");
27+
if (node.left == null && node.right == null) {
28+
res.add(path);
29+
}
30+
if (node.left != null) {
31+
nodePath = new HashMap<String, Object>();
32+
nodePath.put("node", node.left);
33+
nodePath.put("path", path + "->" + String.valueOf(node.left.val));
34+
paths.add(nodePath);
35+
}
36+
if (node.right != null) {
37+
nodePath = new HashMap<String, Object>();
38+
nodePath.put("node", node.right);
39+
nodePath.put("path", path + "->" + String.valueOf(node.right.val));
40+
paths.add(nodePath);
41+
}
42+
}
43+
44+
return res;
45+
}
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {string[]}
11+
*/
12+
var binaryTreePaths = function(root) {
13+
if (root === null) {
14+
return []
15+
}
16+
17+
let res = [],
18+
paths = [
19+
{
20+
node: root,
21+
path: String(root.val)
22+
}
23+
]
24+
25+
while (paths.length > 0) {
26+
let path = paths.pop()
27+
if (path.node.left === null && path.node.right === null) {
28+
res.push(path.path)
29+
}
30+
if (path.node.left !== null) {
31+
paths.push({
32+
node: path.node.left,
33+
path: path.path + '->' + String(path.node.left.val)
34+
})
35+
}
36+
if (path.node.right !== null) {
37+
paths.push({
38+
node: path.node.right,
39+
path: path.path + '->' + String(path.node.right.val)
40+
})
41+
}
42+
}
43+
return res
44+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def binaryTreePaths(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[str]
13+
"""
14+
if not root:
15+
return []
16+
paths = [
17+
{
18+
'node': root,
19+
'path': str(root.val)
20+
}
21+
]
22+
res = []
23+
while len(paths) > 0:
24+
path = paths.pop()
25+
if not path['node'].left and not path['node'].right:
26+
res.append(path['path'])
27+
if path['node'].right:
28+
paths.append({
29+
'node': path['node'].right,
30+
'path': path['path'] + '->' + str(path['node'].right.val)
31+
})
32+
if path['node'].left:
33+
paths.append({
34+
'node': path['node'].left,
35+
'path': path['path'] + '->' + str(path['node'].left.val)
36+
})
37+
return res

0 commit comments

Comments
 (0)