zoukankan      html  css  js  c++  java
  • LeetCode 513. Find Bottom Left Tree Value

    原题链接在这里: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.

    Time Complexity: O(n). n是node个数.

    Space: O(n).

    AC Java:

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public int findBottomLeftValue(TreeNode root) {
    12         LinkedList<TreeNode> que = new LinkedList<TreeNode>();
    13         que.add(root);
    14         while(!que.isEmpty()){
    15             root = que.poll();
    16             if(root.right != null){
    17                 que.add(root.right);
    18             }
    19             if(root.left != null){
    20                 que.add(root.left);
    21             }
    22         }
    23         return root.val;
    24     }
    25 }
  • 相关阅读:
    怎样装两个MySQL服务器
    MySQL 8.0.12的安装与卸载
    位运算符2
    位运算符
    赋值运算符
    love心形
    变量之间运算
    变量
    标识符
    算术运算符
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7568160.html
Copyright © 2011-2022 走看看