Skip to content

Commit 1d0e362

Browse files
committed
add q513 java, cpp, go
1 parent fa79cd4 commit 1d0e362

File tree

6 files changed

+263
-0
lines changed

6 files changed

+263
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
10+
* right(right) {}
11+
* };
12+
*/
13+
class Solution {
14+
public:
15+
int findBottomLeftValue(TreeNode* root) {
16+
return findLeftMostAndDepth(root, 0).first->val;
17+
}
18+
19+
pair<TreeNode*, int> findLeftMostAndDepth(TreeNode* root, int depth) {
20+
bool left = false;
21+
bool right = false;
22+
TreeNode* leftNode = NULL;
23+
TreeNode* rightNode = NULL;
24+
int leftDepth = 0;
25+
int rightDepth = 0;
26+
27+
if (root->left != NULL) {
28+
left = true;
29+
auto p = findLeftMostAndDepth(root->left, depth + 1);
30+
leftDepth = p.second;
31+
leftNode = p.first;
32+
}
33+
34+
if (root->right != NULL) {
35+
right = true;
36+
auto p = findLeftMostAndDepth(root->right, depth + 1);
37+
rightDepth = p.second;
38+
rightNode = p.first;
39+
}
40+
41+
if (left && right) {
42+
if (rightDepth > leftDepth) {
43+
return {rightNode, rightDepth};
44+
} else {
45+
return {leftNode, leftDepth};
46+
}
47+
}
48+
49+
if (left) {
50+
return {leftNode, leftDepth};
51+
}
52+
53+
if (right) {
54+
return {rightNode, rightDepth};
55+
}
56+
57+
return {root, depth};
58+
}
59+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
10+
* right(right) {}
11+
* };
12+
*/
13+
class Solution {
14+
public:
15+
int findBottomLeftValue(TreeNode* root) {
16+
queue<TreeNode*> q;
17+
q.push(root);
18+
19+
while (!q.empty()) {
20+
root = q.front();
21+
q.pop();
22+
23+
if (root->right != NULL) {
24+
q.push(root->right);
25+
}
26+
27+
if (root->left != NULL) {
28+
q.push(root->left);
29+
}
30+
}
31+
32+
return root->val;
33+
}
34+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
12+
type NodeDepth struct {
13+
Node *TreeNode
14+
Depth int
15+
}
16+
17+
func findBottomLeftValue(root *TreeNode) int {
18+
return findBottomLeftAndDepth(root, 0).Node.Val
19+
}
20+
21+
func findBottomLeftAndDepth(root *TreeNode, depth int) NodeDepth {
22+
left := false
23+
right := false
24+
leftDepth := 0
25+
rightDepth := 0
26+
var leftNode *TreeNode
27+
var rightNode *TreeNode
28+
29+
if root.Left != nil {
30+
left = true
31+
nd := findBottomLeftAndDepth(root.Left, depth+1)
32+
leftDepth = nd.Depth
33+
leftNode = nd.Node
34+
}
35+
36+
if root.Right != nil {
37+
right = true
38+
nd := findBottomLeftAndDepth(root.Right, depth+1)
39+
rightDepth = nd.Depth
40+
rightNode = nd.Node
41+
}
42+
43+
if left && right {
44+
if rightDepth > leftDepth {
45+
return NodeDepth{Node: rightNode, Depth: rightDepth}
46+
} else {
47+
return NodeDepth{Node: leftNode, Depth: leftDepth}
48+
}
49+
}
50+
51+
if left {
52+
return NodeDepth{Node: leftNode, Depth: leftDepth}
53+
}
54+
55+
if right {
56+
return NodeDepth{Node: rightNode, Depth: rightDepth}
57+
}
58+
59+
return NodeDepth{Node: root, Depth: depth}
60+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
func findBottomLeftValue(root *TreeNode) int {
12+
q := []*TreeNode{root}
13+
14+
for len(q) != 0 {
15+
root = q[0]
16+
q = q[1:]
17+
18+
if root.Right != nil {
19+
q = append(q, root.Right)
20+
}
21+
22+
if root.Left != nil {
23+
q = append(q, root.Left)
24+
}
25+
}
26+
27+
return root.Val
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode
3+
* right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left,
4+
* TreeNode right) { this.val = val; this.left = left; this.right = right; } }
5+
*/
6+
class Solution {
7+
public class NodeDepth {
8+
public final TreeNode node;
9+
public final int depth;
10+
11+
public NodeDepth(TreeNode node, int depth) {
12+
this.node = node;
13+
this.depth = depth;
14+
}
15+
}
16+
17+
public int findBottomLeftValue(TreeNode root) {
18+
return (findBottomLeftAndDepth(root, 0).node.val);
19+
}
20+
21+
public NodeDepth findBottomLeftAndDepth(TreeNode root, int depth) {
22+
boolean left = root.left != null;
23+
boolean right = root.right != null;
24+
TreeNode leftNode = null;
25+
TreeNode rightNode = null;
26+
int leftDepth = 0;
27+
int rightDepth = 0;
28+
29+
if (left) {
30+
NodeDepth nd = findBottomLeftAndDepth(root.left, depth + 1);
31+
leftNode = nd.node;
32+
leftDepth = nd.depth;
33+
}
34+
35+
if (right) {
36+
NodeDepth nd = findBottomLeftAndDepth(root.right, depth + 1);
37+
rightNode = nd.node;
38+
rightDepth = nd.depth;
39+
}
40+
41+
if (!left && !right) {
42+
return new NodeDepth(root, depth);
43+
}
44+
45+
if (!left) {
46+
return new NodeDepth(rightNode, rightDepth);
47+
}
48+
49+
if (!right) {
50+
return new NodeDepth(leftNode, leftDepth);
51+
}
52+
53+
if (rightDepth > leftDepth) {
54+
return new NodeDepth(rightNode, rightDepth);
55+
}
56+
return new NodeDepth(leftNode, leftDepth);
57+
}
58+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode
3+
* right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left,
4+
* TreeNode right) { this.val = val; this.left = left; this.right = right; } }
5+
*/
6+
class Solution {
7+
public int findBottomLeftValue(TreeNode root) {
8+
Queue<TreeNode> q = new LinkedList<>();
9+
q.add(root);
10+
while (!q.isEmpty()) {
11+
root = q.remove();
12+
13+
if (root.right != null) {
14+
q.add(root.right);
15+
}
16+
17+
if (root.left != null) {
18+
q.add(root.left);
19+
}
20+
}
21+
22+
return root.val;
23+
}
24+
}

0 commit comments

Comments
 (0)