二叉树面试总结 算法 java
2017-10-28 20:45
295 查看
二叉树面试中相关算法,java实现:
package com.js;
import java.util.LinkedList;
import java.util.Stack;
public class BinaryTree {
public class TreeRoot{
int data;
TreeRoot leftChild = null;
TreeRoot rightChild = null;
public TreeRoot(int data){
this.data = data;
}
}
/**
* 递归方式实现
* @param root
*/
private void preOrder(TreeRoot root){
if(root != null){
System.out.println(root.data);
preOrder(root.leftChild);
preOrder(root.rightChild);
}
}
private void inOrder(TreeRoot root){
if(root != null){
inOrder(root.leftChild);
System.out.println(root.data);
inOrder(root.rightChild);
}
}
private void postOrder(TreeRoot root){
if(root != null){
postOrder(root.leftChild);
postOrder(root.rightChild);
System.out.println(root.data);
}
}
/**
* 非递归方式实现
*/
private void noRecPreOrder(TreeRoot root){
Stack<TreeRoot> stack = new Stack();
while(root != null || stack.size() > 0){
while(root != null){
System.out.println(root.data);
stack.push(root);
root = root.leftChild;
}
if(stack.size() > 0){
root = stack.pop();
root = root.rightChild;
}
}
}
private void noRecInOrder(TreeRoot root){
Stack<TreeRoot> stack = new Stack();
while(root != null || stack.size() >0){
while(root != null){
stack.push(root);
root = root.leftChild;
}
if(stack.size() > 0){
root = stack.pop();
System.out.println(root.data);
root = root.rightChild;
}
}
}
private void onRecPostOrder(TreeRoot root){
Stack<TreeRoot> stack = new Stack();
TreeRoot node = root;
while(root != null){
//先左子树遍历
for(;root.leftChild != null;root = root.leftChild){
stack.push(root);
}
//当前节点无右子树 或者 右子树已经输出时
while(root != null && (root.rightChild == null || root.rightChild == node) ){
System.out.println(root.data);
node = root;
if(stack.empty()){
return;
}
root = stack.pop();
}
stack.push(root);
root = root.rightChild;
}
}
private void noRecPastOrder(TreeRoot root){
Stack<TreeRoot> stack = new Stack();
TreeRoot node = root;
while(root != null){
//遍历左子树 使他入栈
for(;root != null;root = root.leftChild){
stack.push(root);
}
//右子树是否等于空 或 右子树已经输出
while(root != null && (root.rightChild ==null || root.rightChild == node)){
System.out.println(root.data);
node = root;
if(stack.empty()){
return;
}
root = stack.pop();
}
stack.push(root);
root = root.rightChild;
}
}
/**
* 求叶子节点个数
* @param root
* @return
*/
private int getNodeNum(TreeRoot root){
if(root == null){
return 0;
}else{
return (getNodeNum(root.leftChild) +
getNodeNum(root.rightChild)) +1;
}
}
/**
* 求二叉树深度
*/
private int getDepth(TreeRoot root){
if(root == null){
return 0;
}else{
int leftDepth = getDepth(root.leftChild);
int rightDepth = getDepth(root.rightChild);
return (leftDepth > rightDepth ? leftDepth:rightDepth)+1;
}
}
/*
* 按层遍历二叉树
*/
private void levelTree(TreeRoot root){
LinkedList<TreeRoot> linkList = new LinkedList<>();
linkList.push(root);
while(!linkList.isEmpty()){
TreeRoot cur = linkList.removeFirst();
System.out.println(cur.data);
if(cur.leftChild != null){
linkList.add(cur.leftChild);
}
if(cur.rightChild != null){
linkList.add(cur.rightChild);
}
}
}
/*
* 求二叉树第K层节点个数
*/
private int getKNum(TreeRoot root ,int k){
if(root == null || k < 1){
return 0;
}
if(k == 1){
return 1;
}
int leftNum = getKNum(root,k-1);
int rightNum = getKNum(root, k-1);
return leftNum+rightNum;
}
/**
* 判断两个二叉树是否相同
*/
public boolean isSameRec(TreeRoot r1,TreeRoot r2){
if(r1 == null && r2 == null){
return true;
}else if(r1 == null || r2 == null){
return false;
}
if(r1.data == r2.data){
return true;
}
boolean leftSame = isSameRec(r1.leftChild,r2.leftChild);
boolean rightSame = isSameRec(r1.rightChild, r2.rightChild);
return leftSame && rightSame;
}
/**
* 检验二叉树是否是平衡二叉树
*/
public boolean isAVLRec(TreeRoot root){
if(root == null){
return true;
}
if(Math.abs(getDepth(root.leftChild) - getDepth(root.rightChild)) > 1){
return false;
}
return isAVLRec(root.leftChild) && isAVLRec(root.rightChild);
}
}
package com.js;
import java.util.LinkedList;
import java.util.Stack;
public class BinaryTree {
public class TreeRoot{
int data;
TreeRoot leftChild = null;
TreeRoot rightChild = null;
public TreeRoot(int data){
this.data = data;
}
}
/**
* 递归方式实现
* @param root
*/
private void preOrder(TreeRoot root){
if(root != null){
System.out.println(root.data);
preOrder(root.leftChild);
preOrder(root.rightChild);
}
}
private void inOrder(TreeRoot root){
if(root != null){
inOrder(root.leftChild);
System.out.println(root.data);
inOrder(root.rightChild);
}
}
private void postOrder(TreeRoot root){
if(root != null){
postOrder(root.leftChild);
postOrder(root.rightChild);
System.out.println(root.data);
}
}
/**
* 非递归方式实现
*/
private void noRecPreOrder(TreeRoot root){
Stack<TreeRoot> stack = new Stack();
while(root != null || stack.size() > 0){
while(root != null){
System.out.println(root.data);
stack.push(root);
root = root.leftChild;
}
if(stack.size() > 0){
root = stack.pop();
root = root.rightChild;
}
}
}
private void noRecInOrder(TreeRoot root){
Stack<TreeRoot> stack = new Stack();
while(root != null || stack.size() >0){
while(root != null){
stack.push(root);
root = root.leftChild;
}
if(stack.size() > 0){
root = stack.pop();
System.out.println(root.data);
root = root.rightChild;
}
}
}
private void onRecPostOrder(TreeRoot root){
Stack<TreeRoot> stack = new Stack();
TreeRoot node = root;
while(root != null){
//先左子树遍历
for(;root.leftChild != null;root = root.leftChild){
stack.push(root);
}
//当前节点无右子树 或者 右子树已经输出时
while(root != null && (root.rightChild == null || root.rightChild == node) ){
System.out.println(root.data);
node = root;
if(stack.empty()){
return;
}
root = stack.pop();
}
stack.push(root);
root = root.rightChild;
}
}
private void noRecPastOrder(TreeRoot root){
Stack<TreeRoot> stack = new Stack();
TreeRoot node = root;
while(root != null){
//遍历左子树 使他入栈
for(;root != null;root = root.leftChild){
stack.push(root);
}
//右子树是否等于空 或 右子树已经输出
while(root != null && (root.rightChild ==null || root.rightChild == node)){
System.out.println(root.data);
node = root;
if(stack.empty()){
return;
}
root = stack.pop();
}
stack.push(root);
root = root.rightChild;
}
}
/**
* 求叶子节点个数
* @param root
* @return
*/
private int getNodeNum(TreeRoot root){
if(root == null){
return 0;
}else{
return (getNodeNum(root.leftChild) +
getNodeNum(root.rightChild)) +1;
}
}
/**
* 求二叉树深度
*/
private int getDepth(TreeRoot root){
if(root == null){
return 0;
}else{
int leftDepth = getDepth(root.leftChild);
int rightDepth = getDepth(root.rightChild);
return (leftDepth > rightDepth ? leftDepth:rightDepth)+1;
}
}
/*
* 按层遍历二叉树
*/
private void levelTree(TreeRoot root){
LinkedList<TreeRoot> linkList = new LinkedList<>();
linkList.push(root);
while(!linkList.isEmpty()){
TreeRoot cur = linkList.removeFirst();
System.out.println(cur.data);
if(cur.leftChild != null){
linkList.add(cur.leftChild);
}
if(cur.rightChild != null){
linkList.add(cur.rightChild);
}
}
}
/*
* 求二叉树第K层节点个数
*/
private int getKNum(TreeRoot root ,int k){
if(root == null || k < 1){
return 0;
}
if(k == 1){
return 1;
}
int leftNum = getKNum(root,k-1);
int rightNum = getKNum(root, k-1);
return leftNum+rightNum;
}
/**
* 判断两个二叉树是否相同
*/
public boolean isSameRec(TreeRoot r1,TreeRoot r2){
if(r1 == null && r2 == null){
return true;
}else if(r1 == null || r2 == null){
return false;
}
if(r1.data == r2.data){
return true;
}
boolean leftSame = isSameRec(r1.leftChild,r2.leftChild);
boolean rightSame = isSameRec(r1.rightChild, r2.rightChild);
return leftSame && rightSame;
}
/**
* 检验二叉树是否是平衡二叉树
*/
public boolean isAVLRec(TreeRoot root){
if(root == null){
return true;
}
if(Math.abs(getDepth(root.leftChild) - getDepth(root.rightChild)) > 1){
return false;
}
return isAVLRec(root.leftChild) && isAVLRec(root.rightChild);
}
}
相关文章推荐
- 面试常用算法总结——排序算法(java版)
- 面试总结:用Java搞定二叉树方面的面试题
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- 【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】
- 【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】
- 面试常用算法总结——排序算法(java版)
- 面试中所有二叉树题目总结(java版)
- 【LeetCode-面试算法经典-Java实现】【226-Invert Binary Tree(反转二叉树)】
- Java经典算法总结—备战面试
- 【LeetCode-面试算法经典-Java实现】【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】
- 面试大总结之二:Java搞定面试中的二叉树题目
- 面试大总结之二:Java搞定面试中的二叉树题目
- 面试大总结之二:Java搞定面试中的二叉树题目
- 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- 面试中的二叉树问题总结【Java版】
- 【数据结构与算法】二叉树的Java实现及特点总结
- 面试大总结之二:Java搞定面试中的二叉树题目
- 面试大总结之二:Java搞定面试中的二叉树题目
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】