1.二叉树的前序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root != null){
list.add(root.val);
//System.out.println(root.val);
List<Integer> leftList = preorderTraversal(root.left);
list.addAll(leftList);
List<Integer> rightList = preorderTraversal(root.right);
list.addAll(rightList);
}
return list;
}
}
2.二叉树的中序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root != null){
//System.out.println(root.val);
List<Integer> leftList = inorderTraversal(root.left);
list.addAll(leftList);
list.add(root.val);
List<Integer> rightList = inorderTraversal(root.right);
list.addAll(rightList);
}
return list;
}
}
3.二叉树的后序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root != null){
//System.out.println(root.val);
List<Integer> leftList = postorderTraversal(root.left);
list.addAll(leftList);
List<Integer> rightList = postorderTraversal(root.right);
list.addAll(rightList);
list.add(root.val);
}
return list;
}
}
4.相同的树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q != null || p !=null && q == null) return false;
if(p == null && q == null) {
return true;
}
if(p.val != q.val) {
return false;
}
boolean left = isSameTree(p.left,q.left);
boolean right = isSameTree(p.right,q.right);
return (left && right);
}
}
5.另一个树的子树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s == null) return false;
if(isSameTree(s,t)) return true;
if(isSubtree(s.left,t)) return true;
if(isSubtree(s.right,t)) return true;
return false;
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q != null || p !=null && q == null) return false;
if(p == null && q == null) {
return true;
}
if(p.val != q.val) {
return false;
}
boolean left = isSameTree(p.left,q.left);
boolean right = isSameTree(p.right,q.right);
return (left && right);
}
}
6.二叉树的最大深度
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
if(root.left == null && root.right == null) {
return 1;
}
int lefthigh = maxDepth(root.left);
int righthigh = maxDepth(root.right);
int max = Math.max(lefthigh,righthigh);
return max+1;
}
}
7.平衡二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(hight(root) >= 0) return true;
return false;
}
public int hight(TreeNode root) {
if(root == null) return 0;
if(root.left == null && root.right == null) {
return 1;
}
int left = hight(root.left);
int right = hight(root.right);
if(left == -1 || right == -1 || Math.abs(left-right) > 1) return -1;
return Math.max(left,right)+1;
}
}
8.对称二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree) {
if(leftTree == null && rightTree != null || leftTree != null && rightTree == null) {
return false;
}
if(leftTree == null && rightTree == null) {
return true;
}
if(leftTree.val != rightTree.val) {
return false;
}
return isSymmetricChild(leftTree.left,rightTree.right) &&
isSymmetricChild(leftTree.right,rightTree.left);
}
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return isSymmetricChild(root.left,root.right);
}
}