leetcode 513. Find Bottom Left Tree Value 一个简单的DFS深度优先遍历
来源:互联网 发布:注册facebook网络错误 编辑:程序博客网 时间:2024/06/15 13:58
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input:
2 / \ 1 3
Output:
1
Example 2:
Input:
1 / \ 2 3 / / \4 5 6 / 7
Output:
7
Note: You may assume the tree (i.e., the given root node) is not NULL.
题意很简单,直接做DFS深度优先遍历即可,在遍历的时候作比较即可
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <cmath>using namespace std;/*struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};*/class Solution {public: int findBottomLeftValue(TreeNode* root) { if (root == NULL) return 0; int maxDepth = 0 , res = root->val; getAll(root,maxDepth,0,res); return res; } void getAll(TreeNode* root, int& maxDepth, int depth,int& res) { if (root == NULL) return; else { getAll(root->left, maxDepth, depth + 1, res); getAll(root->right, maxDepth, depth + 1, res); if (depth > maxDepth) { maxDepth = depth; res = root->val; } return; } }};
阅读全文
0 0
- leetcode 513. Find Bottom Left Tree Value 一个简单的DFS深度优先遍历
- LeetCode---513. Find Bottom Left Tree Value(DFS)
- [DFS]513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- [leetcode]513. Find Bottom Left Tree Value
- [leetcode] 513. Find Bottom Left Tree Value
- 【LeetCode】513. Find Bottom Left Tree Value
- Leetcode 513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- 【leetcode】 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value | LeetCode
- leetcode-513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- leetcode 513. Find Bottom Left Tree Value
- leetcode 513. Find Bottom Left Tree Value
- [leetcode]513. Find Bottom Left Tree Value
- Leetcode 513. Find Bottom Left Tree Value
- 智能指针类的编写
- 《算法导论》学习笔记之Chapter12二叉树基本特点,及二叉搜索树(查找树)
- 用OpenCV的函数convexHull做凸包(凸壳)检测
- Eclipse乱码情况
- 存放用户登录信息 以及 Token 的工具类
- leetcode 513. Find Bottom Left Tree Value 一个简单的DFS深度优先遍历
- n个阶乘相加
- 微信小程序—progress(进度条)
- php常用的扩展组件
- iOS UITableView reloadData刷新的动画
- CMD命令窗口中文乱码问题的解决
- IOS中的NSString与NSArray的互转
- LeetCode--Invert Binary Tree (反转二叉树)Python
- .NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException