二叉树结构:
class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val) {
this.val = val;
}
}
二叉树相关问题整理一:https://blog.csdn.net/qq_43109561/article/details/98028530
目录
1.二叉树的前序遍历
递归解法:
class Solution {
List<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;
}
}
非递归解法:
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;
}
cur = stack.pop().right;
}
return list;
}
}
2.二叉树的中序遍历
递归解法:
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;
}
}
非递归解法:
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode tmp = null;
while(cur != null || !stack.empty()){
while(cur!=null){
stack.push(cur);
cur = cur.left;
}
tmp = stack.pop();
list.add(tmp.val);
cur = tmp.right;
}
return list;
}
}
3.二叉树的后序遍历
递归解法:
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;
}
}
非递归解法:
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode pre = null;
while(cur != null || !stack.empty()){
while(cur!=null){
stack.push(cur);
cur = cur.left;
}
cur = stack.peek().right;
if(cur==null || cur==pre){
pre = stack.pop();
list.add(pre.val);
cur = null;
}
}
return list;
}
}
4.二叉树的层序遍历
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if(root == null){
return result;
}
Queue<TreeNode> queue = new LinkedList<>();
TreeNode cur = root;
queue.offer(cur);
while(!queue.isEmpty()){
int count = queue.size();
List<Integer> list = new ArrayList<>();
while(count!=0){
cur = queue.poll();
if(cur != null){
list.add(cur.val);
if(cur.left != null){
queue.offer(cur.left);
}
if(cur.right != null){
queue.offer(cur.right);
}
}
count--;
}
result.add(list);
}
return result;
}
}
5.从前序与中序遍历序列构造二叉树
class Solution {
int preIndex = 0;
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder==null || inorder==null ||preorder.length==0 || inorder.length==0){
return null;
}
return build(preorder, inorder,0,inorder.length-1);
}
//start-end:inorder数组中查找的范围
public TreeNode build(int[] preorder, int[] inorder,int start,int end) {
if(start > end){
return null;
}
TreeNode root = new TreeNode(preorder[preIndex]);
preIndex++;
int rootIndex = findIndex(inorder,root.val,start,end);
root.left = build(preorder, inorder,start,rootIndex-1);
root.right = build(preorder, inorder,rootIndex+1,end);
return root;
}
public int findIndex(int[] inorder,int num,int start,int end){
for(int i=start; i<=end; i++){
if(inorder[i] == num){
return i;
}
}
return -1;
}
}
6.从后序与中序遍历序列构造二叉树
//postorder从后向前遍历,先建根,再建右树,再建左树
class Solution {
int poIndex = 0;
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder==null || postorder==null ||inorder.length==0 || postorder.length==0){
return null;
}
poIndex = postorder.length-1;
return build(inorder, postorder,0,postorder.length-1);
}
public TreeNode build(int[] inorder, int[] postorder,int start,int end) {
if(start > end){
return null;
}
TreeNode root = new TreeNode(postorder[poIndex]);
poIndex--;
int rootIndex = findIndex(inorder,root.val,start,end);
root.right = build(inorder, postorder,rootIndex+1,end);
root.left = build(inorder, postorder,start,rootIndex-1);
return root;
}
public int findIndex(int[] inorder,int num,int start,int end){
for(int i=start; i<=end; i++){
if(inorder[i] == num){
return i;
}
}
return -1;
}
}
7.二叉搜索树与双向链表
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
public class Solution {
TreeNode pre = null;
public TreeNode Convert(TreeNode pRootOfTree) {
ConvertHelper(pRootOfTree);
while(pRootOfTree!=null && pRootOfTree.left != null){
pRootOfTree = pRootOfTree.left;
}
return pRootOfTree;
}
public void ConvertHelper(TreeNode pRootOfTree) {
if(pRootOfTree == null){
return;
}
ConvertHelper(pRootOfTree.left);
pRootOfTree.left = pre;
if(pre != null){
pre.right = pRootOfTree;
}
pre = pRootOfTree;
ConvertHelper(pRootOfTree.right);
}
}