目录
一、先序遍历二叉树
1、递归遍历
/**
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Integer> result = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
preOrder(root);
return result;
}
public void preOrder(TreeNode root){
if(root != null){
result.add(root.val);
preOrder(root.left);
preOrder(root.right);
}
return;
}
}
2、栈遍历
Java 版本
/**
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode node;
if(root == null){
return result ;
}
stack.push(root);
while(!stack.isEmpty()){
node = stack.pop();
result .add(node.val);
if(node.right != null){
stack.push(node.right);
}
if(node.left != null){
stack.push(node.left);
}
}
return result ;
}
}
Python 版本
def preorderTraversal(root):
result = []
stack = []
if root:
stack.append(root)
while len(stack) > 0:
node = stack.pop()
result.append(node.val)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
return result
二、中序遍历
1、递归遍历
/**
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Integer> result = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
inOrder(root);
return result;
}
public void inOrder(TreeNode root){
if(root != null){
inOrder(root.left);
result.add(root.val);
inOrder(root.right);
}
return;
}
}
2、栈遍历
Java 版本
/**
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result= new ArrayList<>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode node = root;
while(!stack.isEmpty() || node != null){
while(node != null){
stack.push(node);
node = node.left;
}
node = stack.pop();
result.add(node.val);
node = node.right;
}
return result;
}
}
Python 版本
def inorderTraversal(root):
result = []
stack = []
node = root
while len(stack) > 0 or node:
if node:
stack.append(node)
node = node.left
else:
node = stack.pop()
result.append(node.val)
node = node.right
return result
三、后序遍历
1、递归遍历
/**
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Integer> result = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
postOrder(root);
return result;
}
public void postOrder(TreeNode root){
if(root != null){
postOrder(root.left);
postOrder(root.right);
result.add(root.val);
}
return;
}
}
2、栈遍历
Java 版本
/**
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result= new ArrayList<>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode node;
if(root == null){
return result;
}
stack.push(root);
while(!stack.isEmpty()){
node = stack.pop();
result.add(node.val);
if(node.left != null){
stack.push(node.left);
}
if(node.right != null){
stack.push(node.right);
}
}
Collections.reverse(res);
return result;
}
}
Python 版本
def postorderTraversal(self):
result = []
stack = []
if root:
stack.append(root)
while len(stack) > 0:
node = stack.pop()
result.append(node.val)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
result.reverse()
return result
四、层次遍历
1、常规层次遍历
Java 版本
/**
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null)
return new ArrayList<List<Integer>>();
List<List<Integer>> result = new ArrayList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
TreeNode current = root;
queue.offer(root);
while(!queue.isEmpty()){
int count = queue.size();
List<Integer> list = new ArrayList<>();
while(count > 0){
current = queue.poll();
list.add(current.val);
if(current.left != null){
queue.offer(current.left);
}
if(current.right != null){
queue.offer(current.right);
}
count--;
}
result.add(list);
}
return result;
}
}
Python 版本
def levelOrder(root):
result = []
if not root:
return result
queue = [root]
while len(queue) > 0:
result.append([node.val for node in queue])
temp = []
for node in queue:
if node.left:
temp.append(node.left)
if node.right:
temp.append(node.right)
queue = temp
return result
2、层次遍历判断完全二叉树
/**
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null)
return new ArrayList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
TreeNode current = root;
queue.offer(root);
while(!queue.isEmpty()){
current = queue.poll();
if(current != null){
queue.offer(current.left);
queue.offer(current.right);
}
else{
while(!queue.isEmpty()){
current = queue.poll();
if(current != null){
return 0;
}
}
}
}
return 1;
}
}
五、根据先序遍历和中序遍历,建立树
栈版本
def buildTree(preorder, inorder):
if not preorder:
return None
root = TreeNode(preorder[0])
stack = [root]
inorderIndex = 0
for i in range(1, len(preorder)):
preorderVal = preorder[i]
node = stack[-1]
if node.val != inorder[inorderIndex]:
node.left = TreeNode(preorderVal)
stack.append(node.left)
else:
while stack and stack[-1].val == inorder[inorderIndex]:
node = stack.pop()
inorderIndex += 1
node.right = TreeNode(preorderVal)
stack.append(node.right)
return root
递归版本
def find_index(in_start, in_end, inorder, data):
for i in range(in_start, in_end + 1):
if inorder[i] == data:
return i
return 0
def backtracking(pre_start, pre_end, preorder, in_start, in_end, inorder):
if in_start > in_end or pre_start > pre_end:
return None
index = self.find_index(in_start, in_end, inorder, preorder[pre_start])
root = TreeNode(inorder[index])
offset = index - in_start
left = self.backtracking(pre_start + 1, pre_start + offset, preorder, in_start, index - 1, inorder)
right = self.backtracking(pre_start + offset + 1, pre_end, preorder, index + 1, in_end, inorder)
root.left = left
root.right = right
return root
def buildTree(preorder, inorder):
result = self.backtracking(0, len(preorder) - 1, preorder, 0, len(inorder) - 1, inorder)
return result
六、二叉树的拓展
- 镜像二叉树
- 对称二叉树
- 最近公共祖先
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
result = TreeNode(0)
def dfs(self, current, p, q):
if current is None:
return 0
mid = 0
if current.val == p.val or current.val == q.val:
mid = 1
left = self.dfs(current.left, p, q)
right = self.dfs(current.right, p, q)
if left + right + mid == 2:
self. result = current
if left + right + mid >= 1:
return 1
else:
return 0
def lowestCommonAncestor(self, root, p, q):
result = 0
if root is None:
return 0
self.dfs(root, p, q)
return self.result
- 二叉树的直径
# leetcode
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
result = 0
def dfs(self, root):
if root == None:
return 0
left = self.dfs(root.left)
right = self.dfs(root.right)
self.result = max(self.result, left + right)
return max(left, right) + 1
def diameterOfBinaryTree(self, root):
self.dfs(root)
return self.result
- 判断是不是二叉排序树
- 判断是不是二叉平衡树
# leetcode
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def get_high(self, current):
if current is None:
return 0
left = self.get_high(current.left)
right = self.get_high(current.right)
if left == -1 or right == -1 or abs(left - right) > 1:
return -1
else:
return max(left, right) + 1
def isBalanced(self, root):
if root is None:
return True
result = self.get_high(root)
return result != -1
- 最近公共祖先