Skip to content

Commit 8678371

Browse files
committed
solve problem Find Bottom Left Tree Value
1 parent f7d6e9c commit 8678371

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ All solutions will be accepted!
295295
|34|[Find First And Last Position Of Element In Sorted Array](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/)|[java/py/js](./algorithms/FindFirstAndLastPositionOfElementInSortedArray)|Medium|
296296
|75|[Sort Colors](https://leetcode-cn.com/problems/sort-colors/description/)|[java/py/js](./algorithms/SortColors)|Medium|
297297
|623|[Add One Row To Tree](https://leetcode-cn.com/problems/add-one-row-to-tree/description/)|[java/py/js](./algorithms/AddOneRowToTree)|Medium|
298+
|513|[Find Bottom Left Tree Value](https://leetcode-cn.com/problems/find-bottom-left-tree-value/description/)|[java/py/js](./algorithms/FindBottomLeftTreeValue)|Medium|
298299

299300
# Database
300301
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Find Bottom Left Tree Value
2+
This problem is easy to solve by BFS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 int findBottomLeftValue(TreeNode root) {
12+
int val = -1;
13+
LinkedList<TreeNode> stack = new LinkedList<TreeNode>(),
14+
nextStack = new LinkedList<TreeNode>();
15+
16+
stack.add(root);
17+
18+
while (stack.size() > 0) {
19+
TreeNode node = stack.poll();
20+
if (node.right != null)
21+
nextStack.add(node.right);
22+
if (node.left != null)
23+
nextStack.add(node.left);
24+
25+
26+
if (stack.size() == 0) {
27+
val = node.val;
28+
stack = nextStack;
29+
nextStack = new LinkedList<TreeNode>();
30+
}
31+
}
32+
33+
return val;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 {number}
11+
*/
12+
var findBottomLeftValue = function(root) {
13+
let val = -1,
14+
stack = [ root ],
15+
nextStack = []
16+
17+
while (stack.length > 0) {
18+
let node = stack.pop()
19+
20+
if (node.right)
21+
nextStack.unshift(node.right)
22+
if (node.left)
23+
nextStack.unshift(node.left)
24+
25+
if (stack.length == 0) {
26+
val = node.val
27+
stack = nextStack
28+
nextStack = []
29+
}
30+
}
31+
32+
return val
33+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 findBottomLeftValue(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
val = -1
15+
stack = [root]
16+
next_stack = []
17+
18+
while len(stack) > 0:
19+
node = stack.pop()
20+
if node.right:
21+
next_stack.insert(0, node.right)
22+
if node.left:
23+
next_stack.insert(0, node.left)
24+
25+
if len(stack) == 0:
26+
val = node.val
27+
stack = next_stack
28+
next_stack = []
29+
30+
return val

0 commit comments

Comments
 (0)