二叉树
二叉树的种类
-
满二叉树:一棵树中只有度为0和度为2 的节点,并且度为0的节点都在同一层上。
-
完全二叉树:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^(h-1) 个节点。
-
二叉搜索树:
若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
它的左、右子树也分别为二叉排序树
-
平衡二叉搜索树:又被称为AVL(Adelson-Velsky and Landis)树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。(一般是平衡因子起到强制作用)
遍历方式
二叉树主要有两种遍历方式:
- 深度优先遍历:先往深走,遇到叶子节点再往回走。
- 广度优先遍历:一层一层的去遍历。
两种方式
- 深度优先遍历
- 前序遍历(递归法,迭代法)
- 中序遍历(递归法,迭代法)
- 后序遍历(递归法,迭代法)
- 广度优先遍历
- 层次遍历(迭代法)
节点的定义方式
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;
}
}
二叉树的遍历(递归+迭代)
递归:函数调用自己
迭代:通过在循环中改变变量
/**
* 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 list = new ArrayList();
preOrder(root,list);
return list;
}
public void preOrder(TreeNode root, List list){
if(root == null){
return;
}
list.add(root.val);
preOrder(root.left,list);
preOrder(root.right,list);
}
}
理解递归,需要以不同于以往读代码的方式来看。以往读代码的方式是从上往下纵向读,但是递归中的其中几行代码可能会“循环”好几次(这里循环加了引号是想区别于循环, 迭代, 递归
这几个概念下的循环),这时就需要我们从横向的角度读代码. 比如两个preOrder
是平级的, 应当在同一级别理解, 尽量不要掉入"循环"当中.
class Solution {//迭代
public List<Integer> preorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack();
List<Integer> list = new LinkedList();
if(root == null){
return list;
}
stack.push(root);
while(!stack.empty()){
TreeNode node = stack.pop();
list.add(node.val);
if(node.right != null){
stack.push(node.right);
}
if(node.left != null){
stack.push(node.left);
}
}
return list;
}
}
94.二叉树的中序遍历
/**
* 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 list = new LinkedList();
inOrder(root,list);
return list;
}
public void inOrder(TreeNode node,List list){
if(node == null){
return;
}
inOrder(node.left,list);
list.add(node.val);
inOrder(node.right,list);
}
}
中序遍历的迭代不同于前序遍历的迭代, 它的核心思想是通过栈来实现指针的移动. 有中序遍历的性质可知, 我们首先需要找到叶子结点所在那一层的最左边( 哪怕为空 ), 把这一路上的节点全部放入栈中,方便指针进行移动. 如果有节点, 放入栈中, 如果没有节点, 移动到右节点( 弹出栈顶元素 再指向右节点).
class Solution {//迭代
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new LinkedList();
Stack<TreeNode> stack = new Stack();
if(root == null){
return list;
}
TreeNode cur = root;
while(!stack.empty() || cur != null){
if(cur != null){
stack.push(cur);
cur = cur.left;
}
if(cur == null){
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}
return list;
}
}
145.二叉树的后序遍历:
/**
* 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 list = new LinkedList();
postOrder(root,list);
return list;
}
public void postOrder(TreeNode node,List list){
if(node == null){
return;
}
postOrder(node.left,list);
postOrder(node.right,list);
list.add(node.val);
}
}
class Solution {//迭代的方式和前序遍历的迭代差不多,最后反转下就好
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new LinkedList();
Stack<TreeNode> stack = new Stack();
if(root == null){
return list;
}
TreeNode node = root;
stack.push(node);
while(!stack.empty()){
node = stack.pop();
list.add(node.val);
if(node.left != null){
stack.push(node.left);
}
if(node.right != null){
stack.push(node.right);
}
}
Collections.reverse(list);
return list;
}
}
102.层次遍历
如上图所示, 生成一个列表的列表, 使用列表的索引来存储节点.
/**
* 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 {
List<List<Integer>> res = new ArrayList<List<Integer>>();
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null){
return res;
}
checkFun(root,0);
return res;
}
public void checkFun(TreeNode root,int deep){
if(root == null){
return;
}
deep++;
if(res.size() < deep){
List<Integer> list = new ArrayList<Integer>();
res.add(list);
}
res.get(deep - 1).add(root.val);
checkFun(root.left,deep);
checkFun(root.right,deep);
}
}
226.翻转二叉树
通过递归的方式逐层翻转来实现翻转二叉树
/**
* 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 TreeNode invertTree(TreeNode root) {
ArrayDeque<TreeNode> deque = new ArrayDeque<TreeNode>();
if(root == null){
return root;
}
deque.offer(root);
while(!deque.isEmpty()){
int size = deque.size();
TreeNode node = null;
while(size-- > 0){
node = deque.poll();
swap(node);
if(node.left != null) deque.offer(node.left);
if(node.right != null) deque.offer(node.right);
}
}
return root;
}
public void swap(TreeNode root){
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
}
}
101. 对称二叉树
使用递归实现对称二叉树的判断
/**
* 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 isSymmetric(TreeNode root) {
return compare(root.left,root.right);
}
public boolean compare(TreeNode left, TreeNode right){
if(right == null && left == null){
return true;
}
if(right != null && left == null){
return false;
}
if(right == null && left != null){
return false;
}
if(left.val != right.val){
return false;
}
boolean o1 = compare(left.right,right.left);
boolean o2 = compare(left.left,right.right);
return o1 && o2;
}
}