遍历二叉树
1.递归调用二叉树
public static TreeNode dfs(TreeNode root){
System.out.println(root.val);
dfs(root.left);
dfs(root.right);
}
2.非递归调用二叉树
(1)前序遍历
public List preorderTraversal(TreeNode root) {
List res=new ArrayList<>();
Stack stack=new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node=stack.pop();
if(node==null) continue;
res.add(node.val);
stack.push(node.right);//先右后左,保证左子树先进行遍历。
stack.push(node.left);
}
return res;
}
(2)后序遍历
public List preorderTraversal(TreeNode root) {
List res=new ArrayList<>();
Stack stack=new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node=stack.pop();
if(node==null) continue;
res.add(node.val);
stack.push(node.left);
stack.push(node.right);
}
Collections.reverse(res);
return res;
}
(3)中序遍历
public List inorderTraversal(TreeNode root) {
List res=new ArrayList<>();
Stack stack=new Stack<>();
if(root==null)return res;
TreeNode cur=root;
while(cur!=null||!stack.isEmpty()){
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
TreeNode node=stack.pop();
res.add(node.val);
cur=node.right;
}
return res;
}
3.层次遍历二叉树
public class Solution {
public ArrayList PrintFromTopToBottom(TreeNode root) {
ArrayList list = new ArrayList();
if(root==null) return list;
LinkedList queue = new LinkedList();
queue.add(root);
while(!queue.isEmpty()){
TreeNode node = queue.poll();
list.add(node.val);
if(node.left!=null){
queue.addLast(node.left);
}
if(node.right!=null){
queue.addLast(node.right);
}
}
return list ;
}
}
4.层次遍历打印二叉树,每层打印一行
public List> levelOrder(TreeNode root) {
if(root == null)
return new ArrayList<>();
List> res = new ArrayList<>();
Queue queue = new LinkedList();
queue.add(root);
while(!queue.isEmpty()){
int count = queue.size();
List list = new ArrayList();
while(count > 0){
TreeNode node = queue.poll();
list.add(node.val);
if(node.left != null)
queue.add(node.left);
if(node.right != null)
queue.add(node.right);
count--;
}
res.add(list);
}
return res;
}
5.序列化反序列化
public class Solution {
String Serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
if(root == null) {
sb.append("#,");
return sb.toString();
}
sb.append(root.val+",");
sb.append(Serialize(root.left));
sb.append(Serialize(root.right));
return sb.toString();
}
int index = -1;
TreeNode Deserialize(String str) {
index++;
int len=str.length();
String[] strs = str.split(",");
TreeNode root =null;
if(index>=len)
return null;
if(!strs[index].equals("#")) {
root=new TreeNode(Integer.parseInt(strs[index]));
root.left = Deserialize(str);
root.right = Deserialize(str);
}
return root;
}
}
6.判断是不是平衡二叉树
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null){
return true;
}
int nleft=getDept(root.left);
int nright=getDept(root.right);
int diff=nleft-nright;
if(diff<-1||diff>1){
return false;
}
return isBalanced(root.left)&&isBalanced(root.right) ;
}
public int getDept(TreeNode root){
if(root==null){
return 0;
}else{
return Math.max(getDept(root.left),getDept(root.right))+1;
}
}
}
7.二叉树的深度
public int TreeDepth(TreeNode root) {
if(root==null){
return 0;
}
int nleft=TreeDepth(root.left);
int nright=TreeDepth(root.right);
return nleft>nright?(nleft+1):(nright+1);
}
8.二叉搜索树第K节点
public class Solution {
int count=0;
public TreeNode KthNode(TreeNode pRoot,int k){
if(pRoot==null||k<1) return null;
count++;
if(count==k){
return pRoot;
}
TreeNode left=KthNode(pRoot.left, k);
if(left!=null){
return left;
}
TreeNode right=KthNode(pRoot.right, k);
if(right!=null){
return right;
}
return null;
}
}
9.镜像二叉树
public class Solution {
//1.递归的方式建立镜像二叉树
public void Mirror(TreeNode root){
if(root==null){
return;
}
if(root.left==null&&root.right==null){
return;
}else{
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
}
Mirror(root.left);
Mirror(root.right);
}
//2.非递归的方式建立镜像二叉树
public void Mirror1(TreeNode root){
if(root==null){
return;
}
Stack stack=new Stack<>();
while (root!=null||!stack.isEmpty()) {
while (root!=null) {
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
stack.push(root);
root=root.left;
}
}
if (!stack.isEmpty()) {
root=stack.pop();
root=root.right;
}
}
}
10.有前序中序遍历重建二叉树
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if (pre ==null||in==null) {
return null;
}
if (pre.length == 0||in.length == 0) {
return null;
}
if (pre.length != in.length) {
return null;
}
TreeNode root= new TreeNode(pre[0]);
for (int i = 0; i < pre.length; i++) {
if (pre[0]==in[i]) {
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i+1), Arrays.copyOfRange(in, 0, i));
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(in, i+1,in.length));
System.out.print(root.val+",");
}
}
return root;
}
}
11.判断二叉树是否包含子树B
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
boolean result = false;
if(root2!=null&&root1!=null){
if(root1.val==root2.val){
result=Tree1HaveTree2(root1,root2);
}
if(!result){
result= HasSubtree(root1.left,root2)||HasSubtree(root1.right,root2);
}
}
return result;
}
public boolean Tree1HaveTree2(TreeNode node1,TreeNode node2) {
if(node2==null){
return true;
}
if(node1==null){
return false;
}
if(node1.val!=node2.val){
return false;
}
return Tree1HaveTree2(node1.left,node2.left)&&Tree1HaveTree2(node1.right,node2.right);
}
}
12.二叉树的下一个节点
public TreeLinkNode GetNext(TreeLinkNode node){
if(node==null)return null;
if(node.right!=null){
node=node.right;
while(node.left!=null){
node=node.left;
}
return node;
}
while(node.next!=null){
if(node.next.left==node)return node.next;
node=node.next;
}
return null;
}
13.对称二叉树
//思路:先进行左右子树对换,构造出二叉树,然后进行对比数值是否相等。
public class Solution {
boolean isSymmetrical(TreeNode pRoot){
TreeNode node = getMirror(pRoot);
return isSymmetrical(pRoot,node);
}
boolean isSymmetrical(TreeNode pRoot,TreeNode node){
if(pRoot == null && node == null){
return true;
}else if(pRoot == null || node == null){
return false;
}
if(pRoot.val == node.val){
return isSymmetrical(pRoot.left,node.left)&&isSymmetrical(pRoot.right,node.right);
}
return false;
}
TreeNode getMirror(TreeNode pRoot){
if (pRoot == null) {
return null;
}
TreeNode root = new TreeNode(pRoot.val);
root.right = getMirror(pRoot.left);
root.left = getMirror(pRoot.right);
return root;
}
}
14.按照之字形顺序打印二叉树
public ArrayList > Print(TreeNode pRoot) {
ArrayList> res=new ArrayList>();
if(pRoot==null){
return res;
}
ArrayList list;
Queue queue=new LinkedList();
int rows=1;
queue.add(pRoot);
while(!queue.isEmpty()){
list=new ArrayList();
int size=queue.size();
for(int i=0;i