100. Same Tree

Total Accepted: 127501 Total Submissions: 294584 Difficulty: Easy

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
//递归调用,首先判断是否都是空,如果是,则返回true,否则若都不为空,则分两步
//走,如果根节点值相同,则看两者的左节点是否一样,再看右节点,若有不同,则返回false;
//还有就是其他情况:两树中有一树为空的情况,直接返回false
public class Solution {
public boolean isSameTree(TreeNode root1, TreeNode root2){
if(root1==null&&root2==null)
return true;
while(root1!=null&&root2!=null){
if(root1.val==root2.val){
return isSameTree(root1.left, root2.left)&&isSameTree(root1.right, root2.right);
}else{
return false;
}
}
return false;
}
}

101. Symmetric Tree

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

For example, this binary tree is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following is not:

    1
/ \
2 2
\ \
3 3

Note:
Bonus points if you could solve it both recursively and iteratively.

minimum-depth-of-binary-tree

题目描述

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

思路:与求二叉树的深度类似,求二叉树的深度,主要是求二叉树的最长路径,此处求二叉树的最短路径,思路如下:

  1.当root=null时,直接return 0;

  2.当root.left=null且root.right=null时,直接return 1;

  3.当root.left=null或者root.right=null时,递归调用(返回右子树最小长度)run(root.right)+1或(返回左子树最小长度)run(root.left)+1;

  4.最后root.left!=null且root.right!=null时,返回左子树和右子树的最小路径长度;

public class Solution {
public int run(TreeNode root) {
if(root==null)
return 0;
if(root.left==null&&root.right==null)
return 1;
if(root.left==null)
return run(root.right)+1;
if(root.right==null)
return run(root.left)+1;
return Math.min(run(root.left),run(root.right))+1;
}
}
binary-tree-postorder-traversal

Given a binary tree, return the postorder traversal of its nodes' values.

For example:
Given binary tree{1,#,2,3},

   1
\
2
/
3

return[3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

考察二叉树的后序遍历:

非递归思路:使用栈来作为辅助

  思路一:stack作为缓冲,保存左右孩子节点,stack1作为最终保存结果;

    1.当root为空,直接返回空;

    2.当root不为空时,当root入栈stack,直接出栈stack,如果该节点没有左右孩子,直接将该节点入栈stack1,最后出栈;

    3.如果该节点有左右孩子,将其左右孩子依次入栈stack,保存访问过的节点,重复上面的过程,保证每次取栈顶的元素,左孩子在右孩子前面被访问,左孩子和右孩子都在根节点前面被访问。

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<TreeNode> stack2 = new Stack<TreeNode>();
stack.push(root);
while(!stack.empty()){
TreeNode curNode = stack.pop();
if(curNode.left!=null){
stack.push(curNode.left);
}
if(curNode.right!=null){
stack.push(curNode.right);
}
stack2.push(curNode);
}
while(!stack2.empty()){
list.add(stack2.pop().val);
}
return list;
} }

  思路二:对于任一结点P,将其入栈,然后沿其左子树一直往下搜索,直到搜索到没有左孩子的结点,此时该结点出现在栈顶,但是此时不能将其出栈并访问,因此其右孩子还为被访问。所以接下来按照相同的规则对其右子树进行相同的处理,当访问完其右孩子时,该结点又出现在栈顶,此时可以将其出栈并访问。这样就保证了正确的访问顺序。可以看出,在这个过程中,每个结点都两次出现在栈顶,只有在第二次出现在栈顶时,才能访问它。因此需要多设置一个变量标识该结点是否是第一次出现在栈顶。

递归思路:

  1.递归遍历左子树;

  2.递归遍历右子树;

  3.输出根节点;

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
postorderTraversalHelper(root,list);
return list;
}
public void postorderTraversalHelper(TreeNode root, ArrayList<Integer> list){
if(root==null)
return;
postorderTraversalHelper(root.left,list);
postorderTraversalHelper(root.right,list);
list.add(root.val);
}
}
binary-tree-preorder-traversal

Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree{1,#,2,3},

   1
\
2
/
3

return[1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*; public class Solution {
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
preorderTraversalHelper(root, list);
return list;
}
public void preorderTraversalHelper(TreeNode root, ArrayList<Integer> list){
if(root==null)
return;
list.add(root.val);
preorderTraversalHelper(root.left,list);
preorderTraversalHelper(root.right,list);
}
}

LeetCode之二叉树作题java的更多相关文章

  1. LeetCode第[21][23]题(Java):Merge Sorted Lists

    题目:合并两个已排序链表 难度:Easy 题目内容: Merge two sorted linked lists and return it as a new list. The new list s ...

  2. LeetCode之数组处理题java

    342. Power of Four Total Accepted: 7302 Total Submissions: 21876 Difficulty: Easy Given an integer ( ...

  3. LeetCode之字符串处理题java

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

  4. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  5. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  6. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  8. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  9. Java实现 LeetCode 297 二叉树的序列化与反序列化

    297. 二叉树的序列化与反序列化 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得 ...

随机推荐

  1. c/c++指针常见错误

    一 #include <bits/stdc++.h> using namespace std; void f(char *str) { char *s = str; str[] = ' / ...

  2. winform学习目录

    Winform混合式开发框架的特点总结 伍华聪 2016-02-26 10:47 阅读:1966 评论:2     代码生成工具Database2Sharp中增加视图的代码生成以及主从表界面生成功能  ...

  3. STL标准库-容器-rb_tree

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 红黑树,关联式容器底层实现(map set),在使用中基本运用不到,但是还是想了解一下他的运作方式 Red_Black tree ...

  4. 2018.11.28 RF基础1

    1 射频元件 高频电阻: 高频电容: 高频电感: 2 传输线 a 传输线效应:阻抗,3M法则. b 同轴线:RF中用 c 微带线: 常用: 1/4变换线 回波损耗:用网络分析仪测量 插入损耗:传输功率 ...

  5. 6.4-6.5 使用form表单验证,完善登录页面

    之前是使用自定义的类来实现登录逻辑,现在使用django内置的form表单验证,用继承django的view来实现登录页面. users > views.py 的内容是: from django ...

  6. C++17中那些值得关注的特性(上)

    C++17标准在2017上半年已经讨论确定,正在形成ISO标准文档,今年晚些时候会正式发布.本文将介绍最新标准中值得开发者关注的新特新和基本用法. 总的来说C++17相比C++11的新特性来说新特性不 ...

  7. Mysql基本操作(远程登陆,启动,停止,重启,授权)

    1.查看mysql版本 方法一:status; 方法二:select version(); 2.Mysql启动.停止.重启常用命令 a.启动方式 1.使用 service 启动: [root@loca ...

  8. 浅谈一致性Hash原理及应用

    在讲一致性Hash之前我们先来讨论一个问题. 问题:现在有亿级用户,每日产生千万级订单,如何将订单进行分片分表? 小A:我们可以按照手机号的尾数进行分片,同一个尾数的手机号写入同一片/同一表中. 大佬 ...

  9. Libusb学习

    1.参考:http://www.cnblogs.com/Daniel-G/archive/2013/04/22/3036730.html https://baike.so.com/doc/506541 ...

  10. timescaledb replication 使用

    replication 可以确保系统的ha 以及lb 数据的查询,timesacledb 使用pg 内置的stream replication 进行复制的支持 docker 运行参考 https:// ...