*
前序遍历打印二叉树
链接:力扣
递归版本
/**
* 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 {
ArrayList<Integer> list = new ArrayList<>();//注意要定义全局链表
public List<Integer> preorderTraversal(TreeNode root) {
if(root == null){
return list;
}
list.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return list;
}
}
非递归版本:
/**
* 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<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root == null) return list;
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
list.add(cur.val);
cur = cur.left;
}
cur = stack.pop();
cur = cur.right;
}
return list;
}
}
中序遍历二叉树
链接:力扣
递归版本:
/**
* 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<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root == null) return list;
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
return list;
}
}
非递归版本:
/**
* 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<Integer> list = new ArrayList<>();
if(root == null)return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
return list;
}
}
后续遍历二叉树
链接:力扣
递归版本:
/**
* 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<Integer> list = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root == null) return list;
postorderTraversal(root.left);
postorderTraversal(root.right);
list.add(root.val);
return list;
}
}
非递归版本:
/**
* 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<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode prev = null;
while(cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
cur = cur.left;
}
TreeNode tmp = stack.peek();
if(tmp.right == null || tmp.right == prev){
list.add(tmp.val);
stack.pop();
prev = tmp;
}else{
cur = tmp.right;
}
}
return list;
}
}
相同的树
链接:力扣
/**
* 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 isSameTree(TreeNode p, TreeNode q) {
if(p == null && q != null || p != null && q == null){
return false;
}
if(p == null && q == null){
return true;
}
if(p.val != q.val){
return false;
}
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
}
另一个树的子树
链接:力扣
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t){
if(s == null || t ==null) return false;
if(isSameTree(s,t)) return true;
if(isSubtree(s.left,t)) return true;
if(isSubtree(s.right,t)) return true;
return false;
}
public boolean isSameTree(TreeNode s, TreeNode t) {
if(s == null && t == null) return true;
if(s == null || t == null) return false;
if(s.val != t.val) return false;
return isSameTree(s.left,t.left) && isSameTree(s.right,t.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 int maxDepth(TreeNode root) {
if(root == null) return 0;
int leftHigh = maxDepth(root.left)+1;
int rightHigh = maxDepth(root.right)+1;
return Math.max(leftHigh,rightHigh);
}
}
平衡二叉树
链接:力扣
/**
* 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 isBalanced(TreeNode root) {
if(root == null) return true;
int left = high(root.left);
int right = high(root.right);
//int ret = Math.abs(left-right);
if(Math.abs(left-right)>1) return false;
return isBalanced(root.left) && isBalanced(root.right);
}
public int high(TreeNode root){
if(root == null) return 0;
int left = high(root.left)+1;
int right = high(root.right)+1;
return Math.max(left,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 boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return isSymmetricChid(root.left,root.right);
}
public boolean isSymmetricChid(TreeNode rootleft, TreeNode rootright){
if(rootleft == null && rootright ==null){
return true;
}
if(rootleft == null || rootright == null){
return false;
}
if(rootleft.val != rootright.val){
return false;
}
return isSymmetricChid(rootleft.left, rootright.right) &&
isSymmetricChid(rootleft.right, rootright.left);
}
}
前序遍历创建二叉树并中序遍历输出
链接:牛客
import java.util.*;
class TreeNode{
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val){
this.val = val;
}
}
public class Main{
public static int i = 0;
public static TreeNode createTree(String str){
if(str == null || str.length() <= 0) return null;
//int i = 0;
TreeNode root = null;
if(str.charAt(i) != '#'){
root = new TreeNode(str.charAt(i));
i++;
root.left = createTree(str);
root.right = createTree(str);
}else{
i++;
}
return root;//返回值返回null时递归结束
}
public static void inOrderTraverser(TreeNode root){
if(root == null) return;
inOrderTraverser(root.left);
System.out.print(root.val+" ");
inOrderTraverser(root.right);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
TreeNode root = createTree(str);
inOrderTraverser(root);
}
}
二叉树的最近公共祖先
链接:添加链接描述
(LCA)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null|| root == p || root == q) return root;
TreeNode left = lowestCommonAncestor(root.left,p,q);//第一次找到q 或 p 就return了 所以第一次找到的就是公共祖先
TreeNode right = lowestCommonAncestor(root.right,p,q);
if(left==null){
return right;
}
if(right==null){
return left;
}
return root;
}
}
二叉搜索树转为双向链表
链接:牛客
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public TreeNode prev = null;
public void ConverChild(TreeNode cur){
if(cur == null) return;
ConverChild(cur.left);
cur.left = prev;
if(prev != null){
prev.right = cur;
}
prev = cur;
ConverChild(cur.right);
}
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null) return null;
ConverChild(pRootOfTree);
TreeNode head = pRootOfTree;
while(head.left != null){
head = head.left;
}
return head;
}
}
前序遍历与中序遍历序列创建二叉树
链接:添加链接描述
/**
* 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 int preindex = 0;
public TreeNode buildChildTree(int[] preorder,int[] inorder,int inbegin, int inend) {
if(inbegin > inend)return null;
TreeNode root =new TreeNode(preorder[preindex]);
//在中序遍数组 中找到当前位置所在节点
int index = findpreorder(inorder,root.val,inbegin,inend);
preindex++;
root.left = buildChildTree(preorder,inorder,inbegin,index-1);
root.right = buildChildTree(preorder,inorder,index+1,inend);
return root;
}
public int findpreorder(int[] inorder, int key,int inbegin, int inend){
for(int i = inbegin;i <= inend;i++){
if(inorder[i] == key){
return i;
}
}
return -1;
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder == null || inorder == null)return null;
if(preorder.length == 0 || inorder.length == 0)return null;
return buildChildTree(preorder,inorder,0,inorder.length-1);
}
}
后序遍历与中序遍历序列创建二叉树
链接:力扣
/**
* 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 int postindex = 0;
public TreeNode buildChildTree(int[] postorder,int[] inorder,int inbegin, int inend) {
if(inbegin > inend)return null;
TreeNode root = new TreeNode(postorder[postindex]);
//在中序遍数组 中找到当前位置所在节点
int index = findpostorder(inorder,root.val,inbegin,inend);
postindex--;
root.right = buildChildTree(postorder,inorder,index+1,inend);
root.left = buildChildTree(postorder,inorder,inbegin,index-1);
return root;
}
public int findpostorder(int[] inorder, int key,int inbegin, int inend){
for(int i = inbegin;i >= 0;i++){
if(inorder[i] == key){
return i;
}
}
return -1;
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(postorder == null || inorder == null)return null;
if(postorder.length == 0 || inorder.length == 0)return null;
postindex = inorder.length-1;
return buildChildTree(postorder,inorder,0,inorder.length-1);
}
}
根据二叉树创建字符创串
链接:力扣
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。
空节点则用一对空括号 “()” 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
/**
* 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 String tree2str(TreeNode root) {
StringBuilder sb = new StringBuilder();
tree2strChild(root,sb);
return sb.toString();
}
public void tree2strChild(TreeNode t, StringBuilder sb) {
if(t == null) return;
sb.append(t.val);
if(t.left == null){
if(t.right == null){
return;
}else{
sb.append("()");
}
}else{
sb.append("(");
tree2strChild(t.left,sb);
sb.append(")");
}
if(t.right == null){
return;
}else{
sb.append("(");
tree2strChild(t.right,sb);
sb.append(")");
}
}
}
二叉树的层序遍历
链接:力扣
/**
* 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;
* }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list=new ArrayList<>();
if (root==null)
return list;
Queue<TreeNode> q1=new LinkedList<>();
Queue<TreeNode> q2=new LinkedList<>();
q1.offer(root);
while (!q1.isEmpty()){
List<Integer> ret = new ArrayList<>();
while (!q1.isEmpty()){
TreeNode cur = q1.poll();
ret.add(cur.val);
if (cur.left != null)
q2.offer(cur.left);
if (cur.right!=null)
q2.offer(cur.right);
}
list.add(ret);
Queue<TreeNode> tmp = new LinkedList<>(q1);
q1 = q2;
q2 = tmp;
}
return list;
}
}
按之字形顺序打印二叉树
题目描述
链接
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
示例1
输入: {8,6,10,5,7,9,11}
返回值: [[8],[10,6],[5,7,9,11]]
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
if (pRoot == null) return ret;
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
ArrayList<Integer> list = new ArrayList<>();
list.add(pRoot.val);
ret.add(list);
s1.push(pRoot);
while (!s1.isEmpty() || !s2.isEmpty()){
ArrayList<Integer> tmp = new ArrayList<>();
if (!s1.isEmpty()) {
while (!s1.isEmpty()) {
if (s1.peek().right != null) {
tmp.add(s1.peek().right.val);
s2.push(s1.peek().right);
}
if (s1.peek().left != null) {
tmp.add(s1.peek().left.val);
s2.push(s1.peek().left);
}
s1.pop();
}
}else {
while (!s2.isEmpty()) {
if (s2.peek().left != null) {
tmp.add(s2.peek().left.val);
s1.push(s2.peek().left);
}
if (s2.peek().right != null) {
tmp.add(s2.peek().right.val);
s1.push(s2.peek().right);
}
s2.pop();
}
}
if(tmp.size()>0){
ret.add(tmp);
}
}
return ret;
}
}
二叉树的镜像
题目描述:
示例1 输入: {8,6,10,5,7,9,11}
返回值: {8,10,6,11,9,7,5}
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
if(pRoot == null) return null;
if(pRoot.left == null && pRoot.right == null) return pRoot;
TreeNode tmp = pRoot.left;
pRoot.left = pRoot.right;
pRoot.right = tmp;
Mirror(pRoot.left);
Mirror(pRoot.right);
return pRoot;
}
}