LeetCode513
来源:互联网 发布:怎么淘宝放单子 编辑:程序博客网 时间:2024/05/22 15:55
EX. 513
Given a binary tree, find the leftmost value in the last row of the tree.
Example1:
Input: 2 / \ 1 3Output:1
Example2:
Input: 1 / \ 2 3 / / \ 4 5 6 / 7Output:7
Solution:
只需在每一次DFS时,先访问左边,且只在深度大于当前长度时,才更新res的值,并更新当前长度
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int findBottomLeftValue(TreeNode* root) { int bottomLeft = 0; int height = 0; dfs(root, 1, height, bottomLeft); return bottomLeft; }private: void dfs(TreeNode* node, int depth, int& height, int& res) { if (!node) { return; } if (depth > height) { res = node->val; height = depth; } dfs(node->left, depth + 1, height, res); dfs(node->right, depth + 1, height, res); }};
阅读全文
0 0
- leetcode513
- leetcode513
- LeetCode513
- leetcode513 FindBottomLeftTreeValue Java
- LeetCode513. Find Bottom Left Tree Value题解
- leetcode513. Find Bottom Left Tree Value
- Leetcode513. Find Bottom Left Tree Value
- LeetCode513. Find Bottom Left Tree Value
- 24.leetCode513:Find Bottom Left Tree Value
- python--leetcode513. Find Bottom Left Tree Value
- 【Leetcode513 Find Bottom left Tree Value】树的遍历
- pfx证书转base64字符串
- 我和信奥的3年
- Unable to add window -- token android.os.BinderProxy@bf4921f is not valid
- 讲给Android程序员看的前端教程(12)——CSS选择器(2)
- 菜鸟日常错误之——Eclipce中出现错误: 找不到或无法加载主类 HelloWorld1
- LeetCode513
- 遍历文件夹下后缀为csv的文件,保存文件名
- 10-28笔试题
- 数据结构实验之二叉树四:(先序中序)还原二叉树
- linux基础(十五)自动装系统和lftp配置
- 专题模块phpcms
- ArrayList中的实现原理以及实现线程安全
- phpstorm|webstorm配置ES6语法支持和vue.js语法支持
- 数字证书文件格式(cer和pfx)的区别