【LeetCode】Sum of Left Leaves 左叶子之和
来源:互联网 发布:linux vi 替换命令 编辑:程序博客网 时间:2024/05/17 00:03
问题
Find the sum of all left leaves in a given binary tree.
给定一棵二叉树,找出其所有的左叶子节点的值的和。
Example:
3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
思考
这题的关键在于如何找出所有的左叶子节点。而对于一棵二叉树来说,想要找到其所有的左叶子节点,我想到的最直接的方式是遍历这棵二叉树,判断访问到的节点是否为左叶子节点来累加其值,即可得到答案。
解法一
根据我的想法,现在问题转移到了如何遍历获取二叉树的左叶子节点。而遍历一棵二叉树,自然而然的会想到用递归的方式,因为树本身就是一个递归的结构。
二叉树的左叶子节点可能存在于它的左子树和右子树里。从这两部分分别进行讨论求出左叶子节点,再相加即得到所求。
- 如果二叉树的左子树是一个叶子节点,则不用继续深入下去了,要找的就是它。
- 如果二叉树的左子树不是一个叶子节点,则递归调用此过程去获取左子树的左叶子节点的值。
- 而对于二叉树的右子树,则无论其是否是一个叶子节点都不会影响结果,直接递归调用获取右子树的左叶子节点值即可。
按照上述递归的思想,代码如下:
public int sumOfLeftLeaves(TreeNode root) { if (root == null) return 0;//递归结束条件 int left, right; if (root.left != null && root.left.left == null && root.left.right == null) {//判断是否为左叶子节点 left = root.left.val; } else { left = sumOfLeftLeaves(root.left); } right = sumOfLeftLeaves(root.right); return left + right;}
解法二
实际上,遍历二叉树的方式实在是太多了,前序遍历、中序遍历、后序遍历、层级遍历等等。遍历二叉树可以通过递归的方式,也可通过迭代的方式。(话说笔试面试中常常会问到树的非递归遍历…=_=)下面通过前序遍历的迭代方式去遍历二叉树,累加左叶子节点的值来解决问题。
代码如下:
public int sumOfLeftLeaves(TreeNode root) { TreeNode node = root; Stack<TreeNode> stack = new Stack<>(); int sum = 0; while (node != null || !stack.isEmpty()) { while (node != null) { stack.push(node); if (node.left != null && node.left.left == null && node.left.right == null) { sum += node.left.val; } node = node.left; } if (!stack.isEmpty()) { node = stack.pop().right; } } return sum;}
通过一个辅助栈来实现二叉树的非递归前序遍历,在访问到一个节点的时候去判断它是否为左叶子节点即可。
下面再列出层级遍历二叉树的方式去求解的代码:
public int sumOfLeftLeaves(TreeNode root) { if (root == null) return 0; Queue<TreeNode> queue = new ArrayDeque<>(); int sum = 0; queue.offer(root); while (!queue.isEmpty()) { TreeNode node = queue.poll(); if (node.left != null && node.left.left == null && node.left.right == null) { sum += node.left.val; } if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); } return sum;}
思路是一致的,只是层级遍历需要通过一个辅助队列去实现而已。
总结
其实这题挺容易想出解决思路,将求和问题转化为遍历二叉树的问题就行。在这里记录下来,也是为了回顾一下二叉树的几种遍历方式,主要是非递归的几种遍历的写法。
1 0
- 【LeetCode】Sum of Left Leaves 左叶子之和
- [LeetCode] Sum of Left Leaves 左叶子节点的和
- leetcode_404. Sum of Left Leaves-左叶子的和
- Leetcode 404. Sum of Left Leaves 左叶子节点的和 解题报告
- leetcode-404. Sum of Left Leaves 求左叶子节点的和,递归
- [LeetCode]404. Sum of Left Leaves(叶子结点的和)
- 404. Sum of Left Leaves (二叉树中左叶子值之和)
- 404. Sum of Left Leaves求树的所有左叶子节点的和
- Sum of Left Leaves ---LeetCode
- Leetcode Sum of Left Leaves
- LEETCODE--Sum of Left Leaves
- LeetCode : Sum of Left Leaves
- leetcode- Sum of Left Leaves
- LeetCode Sum of Left Leaves
- LeetCode笔记:404. Sum of Left Leaves
- 【leetcode】404. Sum of Left Leaves【E】
- LeetCode—404. Sum of Left Leaves
- [LeetCode]404. Sum of Left Leaves
- 模仿于 b 站番剧列表显示方式,而不止于其显示方式
- 在红尘的最深处重逢
- 面向对象的三个特性
- 100天土鸡饲养计划(65)
- linux下python脚本文件的执行与编码解析
- 【LeetCode】Sum of Left Leaves 左叶子之和
- ubuntu 14.04安装mxnet / CPU实用教程
- C Primer Plus学习 四十 本章中您将学习下列内容
- 索引(从零开始)必须大于或等于零,且小于参数列表的大小
- sqlite高级用法收集
- 分治算法的平面Delauney三角剖分
- weblogic单双向ssl设置
- JDBCExceptionReporter>: OALL8 处于不一致状态!
- 如何在一个jsp页面中弹出另一个jsp页面