<Tree> 337 BFS: 103
337. House Robber III
每个结点有两个结果:
1. result[ 0 ], 不偷, 需要加上子节点的最大值,left[ 0 ] , left[ 1 ] 的最大值 + right[ 0 ], right[ 1 ]
2. result[ 1 ], 偷, 加上子节点不偷的值, left[ 0 ] + right[ 0 ] + 本节点 . val
class Solution {
public int rob(TreeNode root) {
int[] result = robHelper(root);
return Math.max(result[0], result[1]);
}
private int[] robHelper(TreeNode root){
//[0] is max value if not rob current one
//[1] is max value if rob current one
if(root == null) return new int[2];
int result[] = new int[2];
int[] left = robHelper(root.left);
int[] right = robHelper(root.right);
result[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
result[1] = left[0] + right[0] + root.val;
return result;
}
}
103. Binary Tree Zigzag Level Order Traversal
层序遍历的基本BFS算法,加一个level变量,当level为奇数时用Collections.reverse反转。
如果是level order, 用BFS,Queue保存下一个level要处理的节点以保证每一层从左到右遍历的顺序

class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if(root == null) return result;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int level = 0;
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> list = new ArrayList<>();
for(int i = 0; i < size; i++){
TreeNode node = queue.remove();
list.add(node.val);
if(node.left != null){
queue.add(node.left);
}
if(node.right != null){
queue.add(node.right);
}
}
if(level % 2 == 1){
Collections.reverse(list);
}
result.add(list);
level++;
}
return result;
}
}
<Tree> 337 BFS: 103的更多相关文章
- 103. Binary Tree Zigzag Level Order Traversal (Tree, Queue; BFS)
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树
E. Bear and Forgotten Tree 2 题目连接: http://www.codeforces.com/contest/653/problem/E Description A tre ...
- 2018牛客网暑假ACM多校训练赛(第三场)G Coloring Tree 计数,bfs
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-G.html 题目传送门 - 2018牛客多校赛第三场 G ...
- 102. Binary Tree Level Order Traversal (Tree, Queue; BFS)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- [Algorithm] 7. Serialize and Deserialize Binary Tree
Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing t ...
- BFS与DFS常考算法整理
BFS与DFS常考算法整理 Preface BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或 ...
- LeetCode刷题记录(python3)
由于之前对算法题接触不多,因此暂时只做easy和medium难度的题. 看完了<算法(第四版)>后重新开始刷LeetCode了,这次决定按topic来刷题,有一个大致的方向.有些题不止包含 ...
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
随机推荐
- CF53E Dead Ends
CF53E Dead Ends 洛谷评测传送门 题目描述 Life in Bertown has become hard. The city has too many roads and the go ...
- 【STM32H7教程】第25章 STM32H7的TCM,SRAM等五块内存基础知识
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第25章 STM32H7的TCM,SRAM等五块内 ...
- 手动创建分区以及软硬raid的问题
本文章旨在介绍安装centos7时手动分区问题,及其所对应的软硬raid问题.在新建centos7系统时,需要手动分区时,提供给我们三个选项: 在上图中,默认选项为“Standard Partitio ...
- 【shell脚本】nginx启动脚本
[root@localhost init.d]# cat nginx #!/bin/bash #nx Startup script for the Nginx HTTP Server # it ver ...
- pixijs shader 案例
pixijs shader 案例 const app = new PIXI.Application({ transparent: true }); document.body.appendChild( ...
- U-GAT-IT笔记
目录 前言 模型结构 生成器 鉴别器 损失函数 实验结果 结语 由于博客园有时候公式显示不出来,建议在https://github.com/FangYang970206/PaperNote/blob/ ...
- git登陆
git登陆 1. 执行登陆用户名和密码命令 git config --global user.email "you@example.com" git config --global ...
- vue-品牌管理案例
品牌管理 分析 获取到 id 和 name ,直接从 data 上面获取 组织出一个对象 把这个对象,调用 数组的 相关方法,添加到 当前 data 上的 list 中 注意:在Vue中,已经实现了数 ...
- ping、网络抖动与丢包
基本概念: ping: PING指一个数据包从用户的设备发送到测速点,然后再立即从测速点返回用户设备的来回时间.也就是俗称的“网络延迟” 一般以毫秒(ms)计算 一般PING在0~100ms都 ...
- Qt 显示图片
QImage qImag("img.jpg"); //qImag = qImag.scaled(width, height); //缩放图片 qImag = qImag.scale ...