【LeetCode】Sum of Left Leaves 左叶子之和
2016-10-14 09:31
423 查看
问题
Find the sum of all left leaves in a given binary tree.给定一棵二叉树,找出其所有的左叶子节点的值的和。
Example:
3 / \ 9 20 / \ 15 7 There 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; }
思路是一致的,只是层级遍历需要通过一个辅助队列去实现而已。
总结
其实这题挺容易想出解决思路,将求和问题转化为遍历二叉树的问题就行。在这里记录下来,也是为了回顾一下二叉树的几种遍历方式,主要是非递归的几种遍历的写法。相关文章推荐
- LeetCode : Sum of Left Leaves
- leetcode404: 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(java实现)
- Leetcode: Sum of Left Leaves
- LeetCode 404 Sum of Left Leaves (DFS)
- LeetCode之Sum of Left Leaves
- [LeetCode] Sum of Left Leaves 左子叶之和
- leetcode-404-Sum of Left Leaves
- Leetcode 404 Sum of Left Leaves
- LeetCode@HashMap_404_Sum_of_Left_Leaves
- [Leetcode] #404 Sum of Left Leaves
- LeetCode 404 Sum of Left Leaves
- 【LeetCode】404 Sum of Left Leaves(java实现)
- leetcode- Sum of Left Leaves
- leetcode 404 Sum of Left Leaves C++
- LeetCode@Tree_404_Sum_of_Left_Leaves
- leetcode 404 Sum of Left Leaves