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, ...
随机推荐
- [转]How to Download and Setup Blue Prism
本文转自:https://www.hopetutors.com/blog/uncategorized/how-to-download-and-setup-blue-prism/ The Downloa ...
- Php7.3 could not find driver
今天phpstudy升级php7.3,发现框架报错:could not find driver,后来发现默认php.ini的配置有几个是注释掉的,配置php.ini,修改如下 extension=my ...
- C# 创建、更改Excel命名区域(NamedRange)
创建命名区域是指给选定的某个单元格或多个单元格区域设置名称,目的是方便我们在文件中的其他地方对该单元格区域进行引用能够简化公式引用或者方便数据管理.下面记录了具体的C#示例代码.这里创建命名区域分为了 ...
- vue+element-ui实现行数可控的表格输入
element的table中使用 <template slot-scope="scope"> </template> 包裹想要插入的input,或者sele ...
- animate-queue和step-animate
Step-animate: 分为3部分:{配置},{step:function(){...},duration:1000} <div id="warpper" style=& ...
- Android 系统服务的获取与创建
在Android系统中,有一群很厉害的“家伙”,如果把Android系统比喻成一个大帮派,那么这群“家伙”的地位就像那各个分堂的堂主一样,所有的应用就像是各个小马哥,他们要做什么事情,都要堂主审批,审 ...
- Redis基础一(Linux)
Redis概述 1.是一个开源的,先进的<key,value>存储,并用与构建高性能,可扩展的应用程序的完美解决方案 2.从它的许多竞争继承来的三个主要特点: l Redis数据库完全在 ...
- js坚持不懈之15:修改html内容和属性的方法
1. 修改 HTML 内容 <!DOCTYPE html> <html> <body> <p id = "change">原始内容& ...
- shell脚本-正则、grep、sed、awk
----------------------------------------正则---------------------------------------- 基础正则 ^word ##搜索以w ...
- composer在update时提示file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages: error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO
在开发的时候,需要把依赖的服务更新到最新,然后 手动composer update一下,提示如下: failed) Update failed (The "e "https://a ...