513. Find Bottom Left Tree Value
来源:互联网 发布:cmd查找他机mac 编辑:程序博客网 时间:2024/06/05 06:57
Given a binary tree, find the leftmost value in the last row of the
tree.
/** * 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 depth(TreeNode *root) { if(root == NULL){ return 0; } int l = 1 + depth(root->left); int r = 1 + depth(root->right); return l>r?l:r; } int findBottomLeftValue(TreeNode* root) { queue<TreeNode *> qu; int treeDepth = depth(root); TreeNode * node = NULL; if(root == NULL){ return -1; } qu.push(root); while(!qu.empty()){ treeDepth--; if(treeDepth == 0){ node = qu.front(); return node->val; } int n = qu.size(); for(int i = 0;i < n;++i){ TreeNode * node = qu.front(); qu.pop(); if(node->left){ qu.push(node->left); } if(node->right){ qu.push(node->right); } } } return 0; }};
阅读全文
0 0
- LeetCode 513. Find Bottom Left Tree Value
- [leetcode]513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- [leetcode] 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- 【LeetCode】513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- Leetcode 513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- [DFS]513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- 【leetcode】 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value | LeetCode
- 513. Find Bottom Left Tree Value
- LightOJ-1014-Ifter Party
- C语言之断言
- c++中的标准库类型vector
- 使用ffmpeg获取关键帧图片
- 线性代数的本质(Essense Of Linear Algebra)[4]
- 513. Find Bottom Left Tree Value
- pat 乙级 1040. 有几个PAT(25)
- navicat for mysql 10.0.11 简体免安装中文破解版
- 倍增法求最近公共祖先 lca
- two-sum
- Matlab 定义函数的几种方法
- 练习2
- 9.6 今天学到了什么
- Android学习资源