Leetcode 4.28 Tree Easy
1. 101. Symmetric Tree
用递归。
class Solution {
public boolean isSymmetric(TreeNode root) {
if( root == null)
return true;
return symmetric(root.left, root.right);
} public boolean symmetric(TreeNode p, TreeNode q){
if(p == null || q == null)
return p == q? true:false;
return p.val == q.val && symmetric(p.left, q.right) && symmetric(p.right, q.left);
}
}
2. 107. Binary Tree Level Order Traversal II
list表示当前层的所有节点,nextList表示下一层的所有节点。
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList();
List<TreeNode> list = new ArrayList();
if( root == null)
return res;
list.add(root); while(!list.isEmpty()){
List<Integer> curList = new ArrayList();
List<TreeNode> nextList = new ArrayList();
for(TreeNode cur: list){ //循环当前层
curList.add(cur.val);
if(cur.left != null) nextList.add(cur.left);
if(cur.right != null) nextList.add(cur.right);
}
list = nextList;
res.add(0, curList);
}
return res;
}
}
Leetcode 4.28 Tree Easy的更多相关文章
- Leetcode 101. Symmetric Tree(easy)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- [LeetCode] 429. N-ary Tree Level Order Traversal_ Easy
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
随机推荐
- netstat命令总结
nestat介绍 netstat是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字. 使用 ...
- 在java中如何实现字符串的反转
如 "abcdt" 反转之后是 "tdcba" 思路1: 运用递归的方法进行反转 假设反转方法为 reverseString(String str)1)当字符串 ...
- odoo 12企业版与免费社区版的区别,价格策略与技术支持指南的全面解析
Odoo / Ps Cloud收费企业版是对社区版的极大增强,除了增加了很多功能外,最大的功能区别是企业版支持条码而社区版不支持,企业版对手机支持更好.有单独的APP,最重要区别的是企业版提供底层技术 ...
- 自动化测试 Appium之Python运行环境搭建 Part2
Appium之Python运行环境搭建 Part2 by:授客 QQ:1033553122 实践环境 参见 Appium之Python运行环境搭建 Part1 环境部署 1.安装Android SDK ...
- Android为TV端助力 EventBus出现has no public methods called onEvent的问题
Caused by: de.greenrobot.event.EventBusException: Subscriber class com.hhzt.iptv.lvb_w.socket.MyMsgS ...
- vs code配置flutter开发android
下载flutter_sdk压缩包,解压到指定目录,把sdk的bin目录添加到系统环境变量Path 设置中国临时镜像:添加两个系统变量 FLUTTER_STORAGE_BASE_URL=https:// ...
- 教你一步永久激活WebStorm2018
工欲善其事必先利其器,我们在开发过程中,编辑器是我们提高开发效率及生产必备的工具,如何发现一个高效好用的编辑器是程序员必备的技能之一. 前端开发有众多编辑器 sublime.vscode.webstr ...
- Python 经典面试题汇总之网络篇
网络篇 1.简述 OSI 七层协议 物理层:定义物理设备标准,如网线的接口类型.光纤的接口类型.各种传输介质. 数据链路层:定义如何传输格式化数据,以及如何访问物理介质. 网络层:定义逻辑网络地址. ...
- WebApi发布到外网提示404问题
今天在做微信接口的对接,需要把webApi发布到服务器,放上去的时候,提示404 找了以后,发现了这段代码,粘贴上去就可以用了 在web.config添加如下节点 <system.webServ ...
- SQLAchemy模块
老师的博客:http://www.cnblogs.com/wupeiqi/articles/5713330.html 有一篇习详细的博客: http://www.keakon.net/2012/12/ ...