[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
思路:
层次遍历。每一层保存第一个数即可。
int findBottomLeftValue(TreeNode* root) { queue<TreeNode*>que; if (root!=NULL)que.push(root); int ret = -1; while (!que.empty()) { int size = que.size(); for (int i = 0; i < size;i++)//一层 { TreeNode* temp = que.front(); que.pop(); if (i ==0)ret = temp->val; if (temp->left != NULL)que.push(temp->left); if (temp->right != NULL)que.push(temp->right); } } return ret; }