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. JS获取当前时间到30天之后的日期区间

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. CF 272E Dima and Horses 染色,dfs 难度:2

    http://codeforces.com/problemset/problem/272/E 把仇恨关系想象为边, 因为度只能为0,1,2,3,所以有以下几种 0,1 直接放即可 2: 有(1,1), ...

  3. BOM之JavaScript常用事件

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  4. HDFS读写流程

    01.并行读取 02.逐个节点写入

  5. (转载)Nginx/LVS/HAProxy三种主流负载均衡软件的对比

    原地址:http://www.ha97.com/5646.html PS:Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,参考了一些资料,结合自己的一些 ...

  6. 在JavaScript中进行文件处理,第一部分:基础

    译注:原文是<JavaScript高级程序设计>的作者Nicholas Zakas写的,本翻译纯属为自己学习而做,仅供参考.原文链接:这里 很多年前,我在一次Goole面试被问到,如何在w ...

  7. git 忽略不提交的文件3种情形

    1..gitignore文件 :从未提交过的文件,从来没有被 Git 记录过的文件 也就是添加之后从来没有提交(commit)过的文件,可以使用.gitignore忽略该文件.只能作用于未跟踪的文件( ...

  8. OMAP4之DSP核(Tesla)软件开发学习(四)ARM核与DSP核通讯示例

    首先,安卓系统完全启动4AJ.2.1. 其次,查看OMAP4的Tesla相关信息,检查Tesla是否使能.(有如下显示,则OK) cat /d/emoteproc/omap-rproc.0/versi ...

  9. TCP拥塞控制机制

     研究TCP的拥塞机制,不仅仅是想了解TCP如何的精巧,更多的是领悟其设计思想,即在一般情况下,我们该怎样处理问题.   一.拥塞的发生与其不可避免    拥塞发生的主要原因:在于网络能够提供的资源不 ...

  10. Oracle-SQL高级查询

    --一个题目涉及到的50个Sql语句 --(下面表的结构以给出,自己在数据库中建立表.并且添加相应的数据,数据要全面些. 其中Student表中,SId为学生的ID) ---------------- ...