原题链接在这里:
题目:
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input: 2 / \ 1 3Output:1
Example 2:
Input: 1 / \ 2 3 / / \ 4 5 6 / 7Output: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 LinkedListque = new LinkedList ();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 }