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的更多相关文章

  1. Leetcode 101. Symmetric Tree(easy)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  3. [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, ...

  4. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  5. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  6. 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 ...

  7. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  8. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  9. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

随机推荐

  1. 用bat批处理程序通过DOS命令行删除所有的空文件夹

    用过gothub或者码云的同学都知道,不包含任何文件的空文件夹上传提交时不被允许的.当然你可以在空文件下创建.keep文件(或.gitkeep文件),然后就可以上传了. 但是如果空文件夹比较多,并且我 ...

  2. 包、继承以及 LeetCode 27、28题

    1 package.import 和 import static 1.1 Package Java 引入了包(Package)机制,提供了类的多层命名空间,用于解决类的命名冲突.类文件管理问题.Jav ...

  3. Eclipse4JavaEE配置Tomcat运行环境

    如果我们想搭一个网站,我们可以使用Eclipse for JavaEE IDE进行开发. 初次使用需要配置网站的运行环境,可以去Apache官网下载Tomcat 8.5或Tomcat 9的版本 然后打 ...

  4. C# winform 拖拽效果

    //是否在拖拽 bool isDrag = false; //鼠标相对于button控件左上角的坐标 Point contextbtnPoint = Point.Empty; private void ...

  5. iOS----------Xcode 无线调试

    环境要求: 至少Mac OSX 10.12.6 iOS 11 Xcode 9 1. ”自己的工程“ -> windows -> Device and Simulators ,打开设备和模拟 ...

  6. Chromium被用于Microsoft Edge与ChakraCore的未来【译】

    注:英语不好,力求大概能懂.持笔人是:Limin Zhu,好像是中国人,但是没有提供中文版本. 大家好,ChakraCore的朋友们: 昨天,微软公布,Microsoft Edge桌面浏览器采用Chr ...

  7. Delphi中常用字符串处理函数

    .copy(str,pos,num) 从str字符串的pos处开始,截取num个字符的串返回. 假设str为,)=,)='def' .concat(str1,str2{,strn}) 把各自变量连接起 ...

  8. Windows7安装 docker-compose的过程

    Docker在Windows7系统上安装成功后[详情见Windows7下docker的安装以及遇到的问题],要用到docker-compose相关命令,而docker-compose相关命令在dock ...

  9. Install Docker Compose

    https://docs.docker.com/compose/install/ sudo curl -L "https://github.com/docker/compose/releas ...

  10. Help is needed for Dexter UVA - 11384(二分)

    本来抱着wa一发的心态写写,没想到过了. 算是一种二分吧. 也就是说,减数取太大和太小都不好,怎样是最好的呢?当然是,每次减去一个数之后新形成的序列和前面的序一样是最好的 这样的话,本来想写个二分,但 ...