leetcode 513. Find Bottom Left Tree Value

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.

分析:广搜,层序遍历,保存每层的第一个值,最后的即为所求值。

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int findBottomLeftValue(TreeNode* root) {
13         int result = root->val;
14         queue<TreeNode *> q;
15         q.push(root);
16         TreeNode* temp;
17         while (!q.empty()) {
18             int size = q.size();
19             result = q.front()->val;
20             while (size--) {
21                 temp = q.front();
22                 q.pop();
23                 if (temp->left != NULL) q.push(temp->left);
24                 if (temp->right != NULL) q.push(temp->right);
25             }
26         }
27         return result;
28     }
29 };
时间: 2024-10-09 17:41:57

leetcode 513. Find Bottom Left Tree Value的相关文章

[LeetCode]513 Find Bottom Left Tree Value(BFS)

题目链接:https://leetcode.com/problems/find-bottom-left-tree-value/?tab=Description 题意:找到二叉树里最底下的最靠左的叶子节点的值. 看到input的时候吓哭了,以为是要处理这种输入.看到代码填空部分才缓过来- 直接bfs,遇到更深的就更新,每次都让最左的先入队列.就能保证每次更新到的答案都是最左的. 1 /** 2 * Definition for a binary tree node. 3 * struct Tree

513. Find Bottom Left Tree Value - LeetCode

Question 513. Find Bottom Left Tree Value Solution 题目大意: 给一个二叉树,求最底层,最左侧节点的值 思路: 按层遍历二叉树,每一层第一个被访问的节点就是该层最左侧的节点 Java实现: public int findBottomLeftValue(TreeNode root) { Queue<TreeNode> nodeQueue = new LinkedList<>(); nodeQueue.offer(root); // 向

513. Find Bottom Left Tree Value 二叉树左下节点的值

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. 求二叉树左下方节点的值,使用广度优先遍

513. Find Bottom Left Tree Value

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 了吗, 根据size

[LC] 513. Find Bottom Left Tree Value

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. Solution 1: DFS, pr

leetcode算法: Find Bottom Left Tree Value

leetcode算法: Find Bottom Left Tree Value Given a binary tree, find the leftmost value in the last row of the tree. Example 1:Input: 2 / \ 1 3 Output:1Example 2: Input: 1 / \ 2 3 / / \ 4 5 6 / 7 Output:7Note: You may assume the tree (i.e., the given ro

[leetcode]_Maximum Depth of Binary Tree

第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解.看来还得好好研究下递归算法. 题目:求一棵树的最大深度. 思路:递归地求取左子树最大深度 和 右子树最大深度,返回较大值即为 整棵树的 最大深度. 代码: public int maxDepth(TreeNode root) { if(root == null) return 0; int leftHeight = 1,rightHeight = 1; if(root.left != null) leftHeight += maxD

【LeetCode】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Leetcode: Serialize and Deserialize Binary Tree

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput