# 算法

·  阅读 104

## 获得二叉树深度的两种写法

1.每次传入深度，下一层就深度加一。

``````     int Height(TreeNode root,int depth){
if(root==null) return depth;
return Math.max(Height(root.left,depth+1),Height(root.right,depth+1));
}

2.从叶子就1+，回到上层就是深度。

``````    int Height(TreeNode root){
if(root==null) return 0;
return 1+Math.max(Height(root.left),Height(root.right));
}

## 层次遍历

``````//非递归实现
public ArrayList<Integer> printFromTopToBottom(BinaryTreeNode pTreeRoot){ //非递归版本
ArrayList<Integer> list = new ArrayList<Integer>();
if(pTreeRoot == null){
return null;
}
queue.offer(pTreeRoot);
while(!queue.isEmpty()){
BinaryTreeNode treeNode = queue.poll();
if(treeNode.left!=null){
queue.offer(treeNode.left);
}
if(treeNode.right!=null){
queue.offer(treeNode.right);
}
}
return list;
}

``````//二叉树递归实现
private int depth(BinaryTreeNode pTreeRoot){
if(pTreeRoot==null){
return 0;
}
int l = depth(pTreeRoot.left);
int r = depth(pTreeRoot.right);
if(l > r){
return l + 1;
}else{
return r + 1;
}
}

private void levelOrder(BinaryTreeNode pTreeNode, int level) {
if(pTreeNode == null || level < 1){
return ;
}
if(level == 1){
System.out.print(pTreeNode.data+ " ");
return ;
}
//左子树
levelOrder(pTreeNode.left, level-1);
//右子树
levelOrder(pTreeNode.right, level-1);
}

public void printFromTopToBottom(BinaryTreeNode pTreeRoot){
if(pTreeRoot==null){
return ;
}
int depth = depth(pTreeRoot);
for (int i = 1; i <= depth; ++i) {
levelOrder(pTreeRoot, i);
}
}

## 二叉树前序遍历

``````public class PreorderTraversal {
List<Integer> res = new ArrayList<>();
public static void main(String[] args) {

}
//迭代
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
TreeNode node;
while (!stack.isEmpty()){
node = stack.pop();
if (node.right != null){
stack.push(node.right);
}
if (node.left != null){
stack.push(node.left);
}
}

return res;
}
//递归
public void recurse(TreeNode root) {
if (root != null){
recurse(root.left);
recurse(root.right);
}
}
}

## 二叉树中序遍历

``````public class InorderTraversal94 {
List<Integer> res = new ArrayList<>();
public static void main(String[] args) {

}

//迭代
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
while(!stack.isEmpty() || node != null){
while (node != null){
stack.push(node);
node = node.left;
}
node = stack.pop();
node = node.right;
}
return res;
}

//递归
public void recurse(TreeNode root) {
if (root != null){
recurse(root.left);
recurse(root.right);
}
}
}

## 二叉树后序遍历

``````public class PostorderTraversal {
List<Integer> res = new ArrayList<>();
public static void main(String[] args) {

}
//迭代
public static List<Integer> postorderTraversal (TreeNode root){
if (root == null){
return res;
}
TreeNode node;
while (!stack.isEmpty()){
node = stack.pollLast();
if (node.left != null){
}
if (node.right != null){
}
}
return res;
}
//递归
public void recurse(TreeNode root) {
if (root != null){
recurse(root.left);
recurse(root.right);
}
}
}

## 经典算法——数组的循环(左移/右移)K位———思想

### 左移

1.反转前k部分：；{2,1,3,4,5};

2.反转后面部分：{2,1,5,4,3}；

3.整体反转：{3,4,5,1,2}；

### 右移

1.反转前n-k个部分：{3,2,1,4,5}；

2.反转后面部分：{3,2,1,5,4}；

3.整体反转：{4,5,1,2,3}；