目录
1、对称二叉树【OJ链接】
【代码如下】
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return isSymmetricChild(root.left,root.right);
}
public boolean isSymmetricChild(TreeNode left,TreeNode right){
if(left==null&&right==null){
return true;
}
if(left==null||right==null){
return false;
}
if(left.val!=right.val){
return false;
}
return
isSymmetricChild(left.left,right.right)&&isSymmetricChild(left.right,right.left);
}
}
2、创建并遍历二叉树【OJ链接】
【代码如下】
import java.util.Scanner;
//定义二叉树的节点
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 s){
TreeNode root=null;
//字符不为空的情况下,创建根节点
if(s.charAt(i)!='#'){
root=new TreeNode(s.charAt(i));
i++;
//递归创建root的左右子树
root.left=createTree(s);
root.right=createTree(s);
}else{
//字符为空,直接跳过
i++;
}
return root;
}
public static void inorderTree(TreeNode root){
if(root==null){
return;
}
inorderTree(root.left);
System.out.print(root.val+" ");
inorderTree(root.right);
}
//中序遍历二叉树
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()){
String s=in.nextLine();
TreeNode node=createTree(s);
inorderTree(node);
}
}
}
3、二叉树中两节点最近公共祖先【OJ链接】
【代码如下】
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null){
return null;
}
if(root==q||root==p){
return root;
}
TreeNode left=lowestCommonAncestor(root.left,p,q);
TreeNode right=lowestCommonAncestor(root.right,p,q);
if(left==null){
return right;
}
if(right==null){
return left;
}
return root;
}
}
4、二叉搜索树与双向链表【OJ链接】
二叉搜索树:任何节点的左子树小于右子树
【代码如下】
public class Solution {
public TreeNode prev=null;
//中序遍历二叉树
public void inorderTree(TreeNode root){
if(root==null){
return ;
}
inorderTree(root.left);
//处理二叉树的左右节点
root.left=prev;
if(prev!=null){
prev.right=root;
}
prev=root;
inorderTree(root.right);
}
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree==null){
return null;
}
inorderTree(pRootOfTree);
while(pRootOfTree.left!=null){
pRootOfTree=pRootOfTree.left;
}
return pRootOfTree;
}
}
5、根据前序和中序遍历结果创建二叉树【OJ链接】
【代码如下】
class Solution {
public int prevIndex=0;
//找到preorder的prevIndex下标元素在inorder中的位置
public int findIndex(int[] preorder,int[] inorder,int inbegin,int inend){
for(int i=inbegin;i<=inend;++i){
if(inorder[i]==preorder[prevIndex]){
return i;
}
}
return -1;
}
//创建二叉树
public TreeNode buildTreeChild(int[] preorder,int[] inorder,int inbegin,int inend){
if(inbegin>inend){
return null;
}
TreeNode root=new TreeNode(preorder[prevIndex]);
int index=findIndex(preorder,inorder,inbegin,inend);
prevIndex++;
root.left=buildTreeChild(preorder,inorder,inbegin,index-1);
root.right=buildTreeChild(preorder,inorder,index+1,inend);
return root;
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeChild(preorder,inorder,0,inorder.length-1);
}
}
6、二叉树创建字符串【OJ链接】
【代码如下】
class Solution {
public void tree2strChild(TreeNode root,StringBuilder str){
if(root==null){
return;
}
str.append(root.val);
if(root.left!=null){
str.append("(");
tree2strChild(root.left,str);
str.append(")");
}else{
if(root.right==null){
return;
}else{
str.append("()");
}
}
if(root.right==null){
return;
}else{
str.append("(");
tree2strChild(root.right,str);
str.append(")");
}
}
public String tree2str(TreeNode root) {
StringBuilder str=new StringBuilder();
tree2strChild(root,str);
return str.toString();
}
}
7、非递归实现二叉树前序遍历【OJ链接】
【代码如下】
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
TreeNode cur=root;
while(cur!=null||!stack.empty()){
while(cur!=null){
stack.push(cur);
list.add(cur.val);
cur=cur.left;
}
TreeNode node=stack.pop();
cur=node.right;
}
return list;
}
}
8、非递归实现二叉树后序遍历【OJ链接】
【代码如下】
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
TreeNode cur=root;
TreeNode prev=null;
while(cur!=null||!stack.empty()){
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
TreeNode top=stack.peek();
if(top.right==null||top.right==prev){
list.add(top.val);
stack.pop();
prev=top;
}else{
cur=top.right;
}
}
return list;
}
}