LeetCode 513. Find Bottom Left Tree Value

513. Find Bottom Left Tree Value

Given a binary tree, find the leftmost value in the last row of the tree.

题目内容:
给定一个二叉树,找出树中最后一层的最左边的元素。

解题思路:
解决这个题目主要包括2个主要内容:

  1. 遍历二叉树,实现树的遍历可以用BFS和DFS,我这里采用了BFS。
  2. 因为题目要找出二叉树当中最底层的最左边的节点,所以需要知道每个节点属于第几层,和这个节点在这一层中从左到右的顺序,这里不需要知道这个节点在这一层是第几个节点,但是要保证左边的节点的数值要比右边的节点的数值要大。因此,我定义了一个结构体来保存这些信息,结构体如下:
struct QueueNode {
    TreeNode* node; //节点指针
    int height; //当前节点在第几层
    int order; //当前节点在这一层的顺序,要保证的左边的节点要比右边的节点的值小
    QueueNode(TreeNode* n, int h, int o) : node(n), height(h), order(0) {}
};

这个结构体作为进行BFS时queue保存的元素类型,一开始先判断根节点是否为空,如果为空,那函数就直接返回空,否则将保存根节点的QueueNode加入queue当中,这个根节点的height和order都为0。并且将当前的bottomLeft节点初始化为根节点。
当queue不为空的时候,一直从queue中获取一个元素,判断这个元素的height和当前bottomLeft的height,

  • 如果前者比较大,就直接将当前元素保存的节点当作目前的bottomLeft;
  • 如果两者相等,那么判断谁的order小,小的节点当作目前的bottomLeft。

接下来就可以继续判断当前节点是否还存在子节点。

  • 如果当前节点的左子树不为空,那么创建一个QueueNode当如queue中,这个QueueNode的height为当前的height+1,order为当前的order-1;
  • 如果当前节点的右子树不为空,那么创建一个QueueNode放入queue中,这个QueueNode的height为当前的height+1,order为当前的order+1;

当queue为空的时候,也就是遍历完所有节点,那么此时的bottomLeft就是整棵树bottomLeft。

代码:

//
//  main.cpp
//  513. Find Bottom Left Tree Value
//
//  Created by mingjc on 2017/3/16.
//  Copyright © 2017年 mingjc. All rights reserved.
//

#include <iostream>
#include <queue>
using namespace std;

/**
 * Definition for a binary tree node.
 */
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

struct QueueNode {
    TreeNode* node;
    int height;
    int order;
    QueueNode(TreeNode* n, int h, int o) : node(n), height(h), order(0) {}
};

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        if (root == NULL) return NULL;
        queue<QueueNode> q;
        q.push(QueueNode(root, 0, 0));
        QueueNode bottomLeft(root, 0, 0);
        while (!q.empty()) {
            QueueNode node = q.front();
            // cout << "pop " << node.node->val << endl;
            q.pop();

            if (node.height == bottomLeft.height) {
                if (node.order < bottomLeft.order) {
                    bottomLeft = node;
                }
            }
            else if (node.height > bottomLeft.height){
                bottomLeft = node;
            }

            if (node.height <= bottomLeft.height) {
                if (node.node->left != NULL) {
                    // cout << "push " << node.node->left->val << endl;
                    q.push(QueueNode(node.node->left, node.height + 1, node.order - 1));
                }
                if (node.node->right != NULL) {
                    // cout << "push " << node.node->right->val << endl;
                    q.push(QueueNode(node.node->right, node.height + 1, node.order + 1));
                }
            }
        }
        return bottomLeft.node->val;
    }
};

int main(int argc, const char * argv[]) {
    Solution sln;
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
    root->left->left = new TreeNode(4);
    root->left->right = new TreeNode(5);
    root->right->left = new TreeNode(6);
    cout << sln.findBottomLeftValue(root) << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值