[LeetCode]404. Sum of Left Leaves
来源:互联网 发布:网络开发客户的方法 编辑:程序博客网 时间:2024/06/05 01:52
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.
分析:求二叉树左叶子节点的数值之和
解题思路:层序遍历,当遍历到的节点的左子树存在,且该左子树为叶子结点。将它与sum累加求和。
public int sumOfLeftLeaves(TreeNode root) { Queue<TreeNode> queue = new LinkedList<TreeNode>(); int sum = 0; if(root==null)return 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; }
阅读全文
0 0
- 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
- Leetcode 404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves
- 404. Sum of Left Leaves - leetcode
- [LeetCode]--404. 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
- LeetCode 404. Sum of Left Leaves
- leetcode 404. Sum of Left Leaves
- leetcode(88).404. Sum of Left Leaves
- 【LeetCode】 404. Sum of Left Leaves
- leetcode 404. Sum of Left Leaves
- [Leetcode]404. Sum of Left Leaves
- 支付宝表面与银联抢占市场,对标的却还是微信
- UCOSII互斥锁的使用
- [NOIP2017模拟]长跑路径
- Serializable源码翻译
- android 获取当前屏幕方向
- [LeetCode]404. Sum of Left Leaves
- Python—turtle库画图神器
- jQuery Traversing
- 1112: 进制转换(函数专题)
- 实验3 Linux文件管理
- 字符串截取(做项目时的总结-待完善)
- 我的c语言-第一课----打印
- 商务旅行
- HTML5表单新特性探究