513. Find Bottom Left Tree Value
来源:互联网 发布:远程教学软件zoom 编辑:程序博客网 时间:2024/06/05 18:39
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.
又来了一道二叉树的层序遍历的题目
/** * 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) { if (root == NULL) return 0; queue<struct TreeNode*> que1; queue<struct TreeNode*> que2; struct TreeNode* temp; int left = 0; left = root -> val; if (root -> left != NULL) que1.push(root -> left); if (root -> right != NULL) que1.push(root ->right); while (!que1.empty() || !que2.empty()) { if (!que1.empty()) { left = que1.front() -> val; while (!que1.empty()) { temp = que1.front(); if (temp -> left != NULL) que2.push(temp -> left); if (temp -> right != NULL) que2.push(temp -> right); que1.pop(); } } else if (!que2.empty()) { left = que2.front() -> val; while (!que2.empty()) { temp = que2.front(); if (temp -> left != NULL) que1.push(temp -> left); if (temp -> right != NULL) que1.push(temp -> right); que2.pop(); } } } return left; }};
阅读全文
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
- Java--day04 循环语句、方法
- css中的子元素设置绝对定位问题
- 安卓编程之获取系统当前时间
- 从零开发B/S机架应用程序(一)
- 单例模式 随笔
- 513. Find Bottom Left Tree Value
- linux下常用的挂载命令
- Gym 101243.J
- 用Java语言中的socket编程实现jsp页面与网关的链接并且不间断发送控制数据
- 约德尔测试--Bash
- Retrofit的回调封装
- C语言实验——单个字符输入和输出(顺序结构)
- 递归方法入门
- centos6永久修改主机名