作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/find-bottom-left-tree-value/#/description

题目描述

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.

题目大意

求一个二叉树最下面一层的最左边节点。

解题方法

BFS

这就是所谓的BFS算法。广度优先搜索,但是搜索的顺序是有要求的,因为题目要最底层的叶子节点的最左边的叶子,那么进入队列的顺序就是先右节点再左节点,这样能把每层的节点都能从右到左过一遍,那么用一个int保存最后的节点值就可以了。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int findBottomLeftValue(TreeNode root) {
int ans = 0;
Queue<TreeNode> tree = new LinkedList<TreeNode>();
tree.offer(root);
while(!tree.isEmpty()){
TreeNode temp = tree.poll();
if(temp.right != null){
tree.offer(temp.right);
}
if(temp.left != null){
tree.offer(temp.left);
}
ans = temp.val;
}
return ans;
}
}

Python做法是用层次遍历,用的是双向队列,所以注意append和popleft。代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def findBottomLeftValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
q = collections.deque()
q.append(root)
res = []
while q:ruxai
size = len(q)
level = []
for i in range(size):
node = q.popleft()
if not node: continue
level.append(node.val)
q.append(node.left)
q.append(node.right)
if level:
res.append(level)
return res[-1][0]

使用C++用的是BFS版本的层次遍历,代码如下:

/**
* 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) {
queue<TreeNode*> q;
q.push(root);
vector<vector<int>> res;
while (!q.empty()) {
int size = q.size();
vector<int> level;
for (int i = 0; i < size; i++) {
TreeNode* node = q.front(); q.pop();
if (!node) continue;
level.push_back(node->val);
q.push(node->left);
q.push(node->right);
}
if (!level.empty()) {
res.push_back(level);
}
}
return res[res.size() - 1][0];
}
};

DFS

使用DFS很简单了,直接把每一层的元素放到list里面,然后取出最后层的第一个节点即可。至于子树的遍历顺序,需要保证先遍历左子树再遍历右子树,这样才能把最下面一层的最左边节点放到最左侧,至于根节点的值在哪个位置进行append是不重要的。

python代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def findBottomLeftValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
res = []
self.dfs(root, res, 0)
return res[-1][0] def dfs(self, root, res, level):
if not root: return
if level == len(res): res.append([])
res[level].append(root.val)
self.dfs(root.left, res, level + 1)
self.dfs(root.right, res, level + 1)

C++代码需要注意的是,我们应该对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) {
dfs(root, res, 0);
return res[res.size() - 1][0];
}
private:
vector<vector<int>> res;
void dfs(TreeNode* root, vector<vector<int>>& res, int level) {
if (!root) return;
if (level == res.size()) res.push_back({});
res[level].push_back(root->val);
dfs(root->left, res, level + 1);
dfs(root->right, res, level + 1);
}
};

Date

2017 年 4 月 13 日

最新文章

  1. Thinkphp_基础(2)URL模式
  2. 【Alpha版本】冲刺-Day3
  3. [转载]QString 乱谈(3)-Qt5与中文
  4. 使用JavaScript 实现注册表单的校验
  5. s1.charAt(x)==&#39;a&#39;
  6. Linux 2.6的内核编译过程
  7. Spring factorybean
  8. C#编写Windows服务程序图文教程(转载)
  9. (续)顺序表之单循环链表(C语言实现)
  10. Cracking the Coding Interview 第二章
  11. Nest客户端的基本使用方法
  12. [Go] golang连接查询mysql
  13. 不错的anroid源码在线浏览网站【学习笔记】
  14. Vuejs的$watch实现原理
  15. 7-(基础入门篇)关于STM32底层程序使用说明
  16. akka pubsub example
  17. 共享服务Samba,实现liunx与Windows文件共享
  18. Hadoop HDFS分布式文件系统 常用命令汇总
  19. js 使用jquery.form.js文件上传
  20. Codeforces 1106 E. Lunar New Year and Red Envelopes 优先队列+dp

热门文章

  1. 苹果ios通过描述文件获取udid
  2. 字符scanf 的输入注意
  3. 使用flock命令查看nas存储是否支持文件锁
  4. deque、queue和stack深度探索(下)
  5. HTML样式 背景
  6. NSURLConnection和Runloop
  7. redis入门到精通系列(六):redis的事务详解
  8. 并行Louvain社区检测算法
  9. JavaXML解析的四种方法(连载)
  10. Mysql配置 主主同步