目录
BFS
public void BFSWithQueue(TreeNode node){
Queue<TreeNode> queue=new LinkedList<>();
if (node!=null)
queue.offer(node);
while (!queue.isEmpty()){
TreeNode treeNode = queue.poll();
System.out.println(treeNode.val);//在此处理每个节点
if(treeNode.left!=null)
queue.offer(treeNode.left);
if(treeNode.right!=null)
queue.offer(treeNode.right);
}
}
DFS
public void DFSWithStack(TreeNode node) {
if (node == null)
return;
Stack<TreeNode> stack = new Stack<>();
stack.push(node);
while (!stack.isEmpty()) {
TreeNode treeNode = stack.pop();
System.out.println(treeNode.val);//在此处理节点
if (treeNode.right != null)
stack.push(treeNode.right);
if (treeNode.left != null)
stack.push(treeNode.left);
}
}
//递归实现深度优先遍历
public void RecursiveDfs(TreeNode node) {
if (node == null)
return;
System.out.println(node.val);
RecursiveDfs(node.left);
RecursiveDfs(node.right);
}
前序遍历
//递归实现先序遍历
public void preOrder1(TreeNode node) {
if (node != null) {
System.out.println(node.val);
preOrder1(node.left);
preOrder1(node.right);
}
}
public static void preOrder(TreeNode biTree) {//非递归实现
Stack<TreeNode> stack = new Stack<TreeNode>();
while (biTree != null || !stack.isEmpty()) {
while (biTree != null) {
System.out.println(biTree.val);
stack.push(biTree);
biTree = biTree.left;
}
if (!stack.isEmpty()) {
biTree = stack.pop();
biTree = biTree.right;
}
}
}
中序遍历
//递归实现中序遍历
public void inOrder(TreeNode node) {
if (node != null) {
inOrder(node.left);
System.out.println(node.val);
inOrder(node.right);
}
}
//中序遍历非递归实现
public static void midOrder(TreeNode biTree) {
Stack<TreeNode> stack = new Stack<TreeNode>();
while (biTree != null || !stack.isEmpty()) {
while (biTree != null) {
stack.push(biTree);
biTree = biTree.left;
}
if (!stack.isEmpty()) {
biTree = stack.pop();
System.out.println(biTree.val);
biTree = biTree.right;
}
}
}
二叉树的深度
//递归求树的深度
public int depth(TreeNode node){
if(node==null)
return 0;
if(node.left==null&&node.right==null)
return 1;
return 1+Math.max(depth(node.left),depth(node.right));
}
//BFS求树的深度
public int maxDepth(TreeNode root) {
if(root==null)return 0;
Queue<TreeNode> queue=new LinkedList<>();
int count=0;
queue.offer(root);
while(!queue.isEmpty()){
int size=queue.size();
while(size>0){
TreeNode head=queue.poll();
if(head.left!=null){
queue.offer(head.left);
}
if(head.right!=null){
queue.offer(head.right);
}
size--;
}
count++;
}
return count;
}
二叉树的直径
int max=0;
public int diameterOfBinaryTree(TreeNode root) {
if(root==null)
return 0;
depth(root);
return max;
}
public int depth(TreeNode node){
if(node==null) return 0;
int left=depth(node.left);
int right=depth(node.right);
max=Math.max(max,left+right);
//加上根节点本身
return 1+Math.max(left,right);
}
翻转二叉树
public TreeNode Mirror(TreeNode pRoot) {
// write code here
if (pRoot == null)
return pRoot;
TreeNode temp = pRoot.left;
pRoot.left = pRoot.right;
pRoot.right = temp;
Mirror(pRoot.left);
Mirror(pRoot.right);
return pRoot;
}
验证二叉树是否为有效的二叉搜索树
判断二叉树是否是对称的
boolean isSymmetrical(TreeNode pRoot) {
if (pRoot == null)
return true;
return judge1(pRoot.left, pRoot.right);
}
boolean judge1(TreeNode left, TreeNode right) {
if (left == null && right == null)
return true;
if (left == null || right == null)
return false;
if (left.val == right.val)
return judge(left.left, right.right) && judge(left.right, right.left);
else
return false;
}
判断是否为另一个树的子树
public boolean HasSubtree(TreeNode root1, TreeNode root2) {
if (root2 == null || root1 == null)
return false;
if (root1.val == root2.val) {
if (judge(root1, root2))
return true;
}
return HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
}
public boolean judge(TreeNode root1, TreeNode root2) {
if (root2 == null)
return true;
if (root1 == null)
return false;
if (root1.val == root2.val)
return judge(root1.left, root2.left) && judge(root1.right, root2.right);
return false;
}
根据前序和中序遍历重建二叉树
注意前序遍历和中序遍历的特性即可
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
if (pre.length == 0 || in.length == 0)
return null;
TreeNode node = new TreeNode(pre[0]);
for (int i = 0; i < in.length; i++) {
if (in[i] == pre[0]) {
node.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i + 1), Arrays.copyOfRange(in, 0, i + 1));
node.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i + 1, pre.length), Arrays.copyOfRange(in, i + 1, in.length));
}
}
return node;
}
JZ23-数组是否为二叉树的后序遍历结果
public boolean VerifySquenceOfBST(int[] sequence) {
if (sequence == null || sequence.length == 0) return false;
return Judge(sequence, 0, sequence.length - 1);
}
public boolean Judge(int[] sequence, int start, int end) {
if (start >= end)
return true;
int i;
for (i = start; i < end; i++) {
if (sequence[i] > sequence[end])
break;
}
for (int j = i; j < end; j++) {
if (sequence[j] <= sequence[end])
return false;
}
return Judge(sequence, start, i - 1) && Judge(sequence, i, end - 1);
}
判断是否为平衡二叉树
public boolean IsBalanced_Solution(TreeNode root) {
if (root == null)
return true;
if (Math.abs(TreeDepth(root.left) - TreeDepth(root.right)) > 1)
return false;
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
public int TreeDepth(TreeNode node) {
if (node == null)
return 0;
if (node.left == null && node.right == null)
return 1;
return 1 + Math.max(TreeDepth(node.left), TreeDepth(node.right));
}
和为某值的路径
LinkedList<List<Integer>> res = new LinkedList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
recur(root, sum);
return res;
}
void recur(TreeNode root, int tar) {
if(root == null) return;
path.add(root.val);
tar -= root.val;
if(tar == 0 && root.left == null && root.right == null)
res.add(new LinkedList(path));
recur(root.left, tar);
recur(root.right, tar);
path.removeLast();
}
二叉树的所有路径
使用DFS
public List<String> binaryTreePaths(TreeNode root) {
List<String> res=new ArrayList<>();
if(root==null)return res;
DFS(root,"",res);
return res;
}
public void DFS(TreeNode node,String path,List<String> res){
if(node!=null){
StringBuilder pathSB=new StringBuilder(path);
pathSB.append(node.val+"");
if(node.left==null&&node.right==null)
res.add(pathSB.toString());
else
pathSB.append("->");
DFS(node.left,pathSB.toString(),res);
DFS(node.right,pathSB.toString(),res);
}
}
236.二叉树的最近公共祖先
题目链接
思路:
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null)return null;
if(root==p||root==q)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;
else
return root;
}