二叉树前序,中序,后序递归非递归
二叉树的前序遍历、中序遍历、后序遍历---java递归非递归实现
//前序遍历
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root != null) stack.push(root);
while (!stack.isEmpty() ) {
root = stack.pop();
list.add(root.val);
if(root.right != null) stack.push(root.right);
if(root.left != null) stack.push(root.left);
}
return list;
}
//中序遍历
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while ( cur != null || !stack.isEmpty() ) {
while ( cur != null ) {
stack.push(cur);
cur = cur.left;
}
TreeNode temp = stack.pop();
list.add(temp.val);
cur = temp.right;
}
return list;
}
//后序遍历
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
Stack<TreeNode> output = new Stack<>();
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
output.push(node);
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
while(!output.isEmpty()){
list.add(output.pop().val);
}
return list;
}
二叉树按层从左到右打印,从顶部到底部
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> lists = new ArrayList<>();
if (root == null) return lists;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
while ( !queue.isEmpty() ) {
int cur = 0;
int end = queue.size();
ArrayList<Integer> list = new ArrayList<>();
while ( cur < end ) {
TreeNode temp = queue.pop();
list.add(temp.val);
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
cur++;
}
lists.add(list);
}
return lists;
}
}
二叉树按层,从底部到顶部
public List<List<Integer>> levelOrder(TreeNode root) {
LinkedList<List<Integer>> lists = new LinkedList<>();
if (root == null) return lists;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
while ( !queue.isEmpty() ) {
int cur = 0;
int end = queue.size();
ArrayList<Integer> list = new ArrayList<>();
while ( cur < end ) {
TreeNode temp = queue.pop();
list.add(temp.val);
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
cur++;
}
lists.push(list);
}
return lists;
}
二叉树深度
public int maxDepth(TreeNode root) {
if (root == null) return 0;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
int level = 0;
while ( !queue.isEmpty() ) {
int cur = 0;
int end = queue.size();
while ( cur < end ) {
TreeNode temp = queue.pop();
cur++;
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
}
level++;
}
return level;
}
递归版
public int TreeDepth(TreeNode root) {
if(root == null) return 0;
int left = TreeDepth(root.left);
int right = TreeDepth(root.right);
return Math.max(left, right) + 1;
}
二叉树之字形打印
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> lists = new ArrayList<>();
if (root == null) return lists;
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.add(root);
int level = 1;
while ( !stack1.isEmpty() || !stack2.isEmpty() ) {
List<Integer> list = new ArrayList<>();
if (level % 2 == 1) {
while ( !stack1.isEmpty() ) {
TreeNode temp = stack1.pop();
list.add(temp.val);
if (temp.left != null) {
stack2.push(temp.left);
}
if (temp.right != null) {
stack2.push(temp.right);
}
}
}else {
while ( !stack2.isEmpty() ) {
TreeNode temp = stack2.pop();
list.add(temp.val);
if (temp.right != null) {
stack1.push(temp.right);
}
if (temp.left != null) {
stack1.push(temp.left);
}
}
}
lists.add(list);
level++;
}
return lists;
}
对称二叉树
public boolean isSymmetric(TreeNode root) {
return isSymmetric(root, root);
}
private boolean isSymmetric(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) {
return true;
}
if (root1 == null || root2 == null) {
return false;
}
if (root1.val != root2.val) {
return false;
}
return isSymmetric(root1.left, root2.right) && isSymmetric(root1.right, root2.left);
}
翻转的二叉树(镜像二叉树)
public TreeNode invertTree(TreeNode root) {
if (root == null) return null;
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
// 镜像二叉树
public void mirror(TreeNode root) {
if (root == null) return;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
if (root.left != null) {
mirror(root.left);
}
if (root.right != null) {
mirror(root.right);
}
}
判断是否是平衡二叉树
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
// 先判断是不是左树和右树的高度差是不是1,接着判断左树和右树是不是平衡二叉树。
if (Math.abs(getDeep(root.left) - getDeep(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right)) {
return true;
}else {
return false;
}
}
private int getDeep(TreeNode root) {
if (root == null) return 0;
int left = getDeep(root.left);
int right = getDeep(root.right);
return (left > right ? left : right) + 1;
}