diff options
Diffstat (limited to 'find_bottom_left_tree.c')
-rw-r--r-- | find_bottom_left_tree.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/find_bottom_left_tree.c b/find_bottom_left_tree.c new file mode 100644 index 0000000..7d6231d --- /dev/null +++ b/find_bottom_left_tree.c @@ -0,0 +1,35 @@ +// 513. Find Bottom Left Tree Value +#include "leetcode.h" + +/* + * given the 'root' of a binary tree, return the leftmost value in the last row + * of the tree + */ + +struct TreeNode { + int val; + struct TreeNode *left; + struct TreeNode *right; +}; + +int depth(struct TreeNode *root) { + if (!root) + return 0; + else + return 1 + fmax(depth(root->left), depth(root->right)); +} + +void dfs(struct TreeNode *root, int len, int *res) { + if (!root) + return; + if (!len) + *res = root->val; + dfs(root->right, len - 1, res); + dfs(root->right, len - 1, res); +} + +int findBottomLeftValue(struct TreeNode *root) { + int len = depth(root), ans = 0; + dfs(root, len - 1, &ans); + return ans; +} |