/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// 递归函数: 参数是被看做是根节点的当前节点, 返回值是当前加点的左孩子结点的和
public int sumOfLeftLeaves(TreeNode root) {
//递归出口(当前节点为叶结点了, 只能退出了)
if(root == null)return 0;
//单层逻辑
int sum = 0;
if(root.left != null && root.left.left == null && root.left.right == null)
sum += root.left.val;
sum += sumOfLeftLeaves(root.left);
sum += sumOfLeftLeaves(root.right);
//返回值;
return sum;
}
}
[思路分析二, 迭代法]
- 使用层序遍历遍历每一层, 当从队列中取出一个node时(我们看做是root), 如果这个node存在左叶子结点, 直接把他的值叠加到sum中, 然后把node的左右子树放入队列, 以进行下一层的迭代;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
//层序迭代法
Queue<TreeNode> queue = new LinkedList<>();
if(root == null)return 0;
queue.add(root);
int sum = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
root = queue.poll();
if(root.left != null){
if(root.left.left == null && root.left.right == null){
sum += root.left.val;
}
queue.add(root.left);
}
if(root.right != null){
queue.add(root.right);
}
}
}
return sum;
}
}
lt.513. 找树左下角的值
[案例需求]
[思路分析一, 递归]
- 待补充
public int findBottomLeftValue(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
TreeNode poll = null;
while (!deque.isEmpty()){
poll = deque.poll();
if (poll.right != null){
deque.offer(poll.right);
}
if (poll.left != null){
deque.offer(poll.left);
}
}
return poll.val;
}
}
[思路分析二, 层序遍历修修补补]
[代码实现]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
//函数, 找到最下面的左叶子结点, 返回的是这个节点的值
public int findBottomLeftValue(TreeNode root) {
//BFS
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
//记录每一层最左边的值
int ans = 0;
while(!queue.isEmpty()){
int size = queue.size(); // 每一层的结点个数
for(int i = 0; i < size; i++){
root = queue.poll();
//最后一层
if(i == 0){
ans = root.val;
}
if(root.left != null)queue.add(root.left);
if(root.right != null)queue.add(root.right);
}
}
return ans;
}
}
[思路分析三, 迭代]
- BFS,题目要求输出最底层最左的元素,我们只需要先序广度搜索这棵树,并把元素分别入队出队,最后出队的那个就是那个最底层最左的元素了。
public int findBottomLeftValue(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
TreeNode poll = null;
while (!deque.isEmpty()){
poll = deque.poll();
if (poll.right != null){
deque.offer(poll.right);
}
if (poll.left != null){
deque.offer(poll.left);
}
}
return poll.val;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)