1.判断两个数是否相同
题目链接:. - 力扣(LeetCode)
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q!=null || q==null && p!=null){
return false;
}
if(p==null && q==null){
return true;
}
if(q.val!=p.val){
return false;
}
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
2.另一颗的子树
题目链接:. - 力扣(LeetCode)
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if(root==null){
return false;
}
if(isSameTree(root,subRoot)){
return true;
}
if(isSubtree(root.left,subRoot)){;
return true;
}
if(isSubtree(root.right,subRoot)){;
return true;
}
return false;
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q!=null || q==null && p!=null){
return false;
}
if(p==null && q==null){
return true;
}
if(q.val!=p.val){
return false;
}
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
}
3.翻转二叉树
题目链接:. - 力扣(LeetCode)
public TreeNode invertTree(TreeNode root) {
if(root==null){
return null;
}
TreeNode tem=null;
tem=root.left;
root.left=root.right;
root.right=tem;
invertTree(root.left);
invertTree(root.right);
return root;
}
4.对称二叉树
题目链接:. - 力扣(LeetCode)
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return isChild(root.left,root.right);
}
public boolean isChild(TreeNode a1,TreeNode a2){
if(a1==null && a2!=null || a1!=null && a2==null){
return false;
}
if(a1==null && a2==null){
return true;
}
if(a1.val!=a2.val ){
return false;
}
return isChild(a1.left,a2.right)&&isChild(a1.right,a2.left);
}
5.平衡二叉树
题目链接:. - 力扣(LeetCode)


public int getHeight(TreeNode root){
if(root==null){
return 0;
}
int leftHeight=getHeight(root.left);
if(leftHeight<0){
return -1;
}
int rightHeight=getHeight(root.right);
if(rightHeight>=0 && Math.abs(leftHeight-rightHeight)<=1){
return Math.max(leftHeight,rightHeight)+1;
}else {
return -1;
}
}
6.二叉树的遍历
题目链接:二叉树遍历_牛客题霸_牛客网
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
String str=in.nextLine();
TreeNode root=creatTree(str);
inorderTree(root);
}
}
public static int i=0;
public static TreeNode creatTree(String str){
TreeNode root=null;
if(str.charAt(i)!='#'){
root=new TreeNode(str.charAt(i));
i++;
root.left=creatTree(str);
root.right=creatTree(str);
}else{
i++;
}
return root;
}
public static void inorderTree(TreeNode root){
if(root==null){
return ;
}
inorderTree(root.left);
System.out.print(root.val+" ");
inorderTree(root.right);
}
7.二叉树的分层遍历
题目链接:. - 力扣(LeetCode)

class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret=new ArrayList<>();
if(root==null){
return ret;
}
Queue<TreeNode> queue=new LinkedList<>();
queue.offer(root);
while(!queue.empty()){
int size=queue.size();
List<Character> list=new List<>();
while(size!=0){
TreeNode<Character> cur=queue.poll();
list.add(cur.val);
if(cur.left!=null){
queue.offer(cur.left);
}
if(cur.right!=null){
queue.offer(cur.right);
}
size--;
}
ret.add(list);
}
return ret;
}
}
希望能对大家有所帮助!!!!!!

881

被折叠的 条评论
为什么被折叠?



